How to use does_not_fail_when_rule_invokes_statement_multiple_times method of org.mockito.internal.runners.DefaultInternalRunnerTest class

Best Mockito code snippet using org.mockito.internal.runners.DefaultInternalRunnerTest.does_not_fail_when_rule_invokes_statement_multiple_times

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright (c) 2017 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.runners;
6
7import static org.junit.Assert.assertNotNull;
8import static org.junit.Assert.assertTrue;
9import static org.mockito.ArgumentMatchers.any;
10import static org.mockito.Mockito.*;
11
12import org.junit.Rule;
13import org.junit.Test;
14import org.junit.rules.TestRule;
15import org.junit.runner.Description;
16import org.junit.runner.notification.Failure;
17import org.junit.runner.notification.RunListener;
18import org.junit.runner.notification.RunNotifier;
19import org.junit.runners.model.Statement;
20import org.mockito.Mock;
21import org.mockito.internal.junit.MockitoTestListener;
22import org.mockito.internal.junit.TestFinishedEvent;
23import org.mockito.internal.util.Supplier;
24
25public class DefaultInternalRunnerTest {
26
27    private final RunListener runListener = mock(RunListener.class);
28    private final MockitoTestListener mockitoTestListener = mock(MockitoTestListener.class);
29    private final Supplier<MockitoTestListener> supplier =
30            new Supplier<MockitoTestListener>() {
31                public MockitoTestListener get() {
32                    return mockitoTestListener;
33                }
34            };
35
36    @Test
37    public void does_not_fail_when_tests_succeeds() throws Exception {
38        new DefaultInternalRunner(SuccessTest.class, supplier).run(newNotifier(runListener));
39
40        verifyTestFinishedSuccessfully();
41    }
42
43    @Test
44    public void does_not_fail_second_test_when_first_test_fail() throws Exception {
45        new DefaultInternalRunner(TestFailOnInitialization.class, supplier)
46                .run(newNotifier(runListener));
47
48        verify(runListener, times(1)).testFailure(any(Failure.class));
49        verify(runListener, times(1)).testFinished(any(Description.class));
50        verify(mockitoTestListener, only()).testFinished(any(TestFinishedEvent.class));
51
52        reset(runListener, mockitoTestListener);
53
54        new DefaultInternalRunner(SuccessTest.class, supplier).run(newNotifier(runListener));
55
56        verifyTestFinishedSuccessfully();
57    }
58
59    @Test
60    public void does_not_fail_when_rule_invokes_statement_multiple_times() throws Exception {
61        new DefaultInternalRunner(TestWithRepeatingRule.class, supplier)
62                .run(newNotifier(runListener));
63
64        verifyTestFinishedSuccessfully();
65    }
66
67    private void verifyTestFinishedSuccessfully() throws Exception {
68        verify(runListener, never()).testFailure(any(Failure.class));
69        verify(runListener, times(1)).testFinished(any(Description.class));
70        verify(mockitoTestListener, only()).testFinished(any(TestFinishedEvent.class));
71    }
72
73    private RunNotifier newNotifier(RunListener listener) {
74        RunNotifier notifier = new RunNotifier();
75        notifier.addListener(listener);
76        return notifier;
77    }
78
79    public static class SuccessTest {
80
81        @Test
82        public void this_test_is_NOT_supposed_to_fail() {
83            assertTrue(true);
84        }
85    }
86
87    public static final class TestFailOnInitialization {
88
89        @Mock private System system;
90
91        @Test
92        public void this_test_is_supposed_to_fail() {
93            assertNotNull(system);
94        }
95    }
96
97    public static final class TestWithRepeatingRule extends SuccessTest {
98
99        @Rule
100        public TestRule rule =
101                (base, description) ->
102                        new Statement() {
103                            @Override
104                            public void evaluate() throws Throwable {
105                                base.evaluate();
106                                base.evaluate();
107                            }
108                        };
109    }
110}
111
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger does_not_fail_when_rule_invokes_statement_multiple_times code on LambdaTest Cloud Grid

Execute automation tests with does_not_fail_when_rule_invokes_statement_multiple_times on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie