1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.runners; 6 7 import org.junit.Test; 8 import org.junit.runner.Description; 9 import org.junit.runner.notification.Failure; 10 import org.junit.runner.notification.RunListener; 11 import org.junit.runner.notification.RunNotifier; 12 import org.mockito.Mock; 13 import org.mockito.internal.junit.MockitoTestListener; 14 import org.mockito.internal.junit.TestFinishedEvent; 15 import org.mockito.internal.util.Supplier; 16 17 import static org.junit.Assert.assertNotNull; 18 import static org.junit.Assert.assertTrue; 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.*; 21 22 public class DefaultInternalRunnerTest { 23 24 private final RunListener runListener = mock(RunListener.class); 25 private final MockitoTestListener mockitoTestListener = mock(MockitoTestListener.class); 26 private final Supplier<MockitoTestListener> supplier = new Supplier<MockitoTestListener>() { 27 public MockitoTestListener get() { 28 return mockitoTestListener; 29 } 30 }; 31 32 @Test does_not_fail_when_tests_succeeds()33 public void does_not_fail_when_tests_succeeds() throws Exception { 34 new DefaultInternalRunner(SuccessTest.class, supplier) 35 .run(newNotifier(runListener)); 36 37 verify(runListener, never()).testFailure(any(Failure.class)); 38 verify(runListener, times(1)).testFinished(any(Description.class)); 39 verify(mockitoTestListener, only()).testFinished(any(TestFinishedEvent.class)); 40 } 41 42 @Test does_not_fail_second_test_when_first_test_fail()43 public void does_not_fail_second_test_when_first_test_fail() throws Exception { 44 new DefaultInternalRunner(TestFailOnInitialization.class, supplier) 45 .run(newNotifier(runListener)); 46 47 verify(runListener, times(1)).testFailure(any(Failure.class)); 48 verify(runListener, never()).testFinished(any(Description.class)); 49 verify(mockitoTestListener, never()).testFinished(any(TestFinishedEvent.class)); 50 51 reset(runListener); 52 53 new DefaultInternalRunner(SuccessTest.class, supplier) 54 .run(newNotifier(runListener)); 55 56 verify(runListener, never()).testFailure(any(Failure.class)); 57 verify(runListener, times(1)).testFinished(any(Description.class)); 58 verify(mockitoTestListener, only()).testFinished(any(TestFinishedEvent.class)); 59 } 60 newNotifier(RunListener listener)61 private RunNotifier newNotifier(RunListener listener) { 62 RunNotifier notifier = new RunNotifier(); 63 notifier.addListener(listener); 64 return notifier; 65 } 66 67 public static final class SuccessTest { 68 69 @Test test()70 public void test() { 71 assertTrue(true); 72 } 73 } 74 75 public static final class TestFailOnInitialization { 76 77 @Mock 78 private System system; 79 80 @Test test()81 public void test() { 82 assertNotNull(system); 83 } 84 } 85 } 86