How to use method of org.jmock.junit5.acceptance.FailureRecordingTestExecutionListener class

Best Jmock-library code snippet using org.jmock.junit5.acceptance.FailureRecordingTestExecutionListener.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.junit5.acceptance;
2
3import org.jmock.junit5.testdata.jmock.acceptance.DerivedJUnit5TestThatDoesNotSatisfyExpectations;
4import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatAutoInstantiatesMocks;
5import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatCreatesNoMockery;
6import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatCreatesTwoMockeries;
7import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatDoesNotCreateAMockery;
8import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatDoesNotSatisfyExpectations;
9import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatDoesSatisfyExpectations;
10import org.jmock.junit5.testdata.jmock.acceptance.JUnit5TestThatThrowsExpectedException;
11import org.junit.jupiter.api.Test;
12
13/**
14 * Wrap, running "testdata" testcases. Some of which are supposed to fail
15 * @author oliverbye
16 *
17 */
18public class JUnit5TestRunnerTests {
19
20    FailureRecordingTestExecutionListener listener = new FailureRecordingTestExecutionListener();
21
22    @Test
23    public void testTheJUnit5TestRunnerReportsPassingTestsAsSuccessful() {
24        listener.runTestIn(JUnit5TestThatDoesSatisfyExpectations.class);
25        listener.assertTestSucceeded();
26    }
27
28    @Test
29    public void testTheJUnit5TestRunnerAutomaticallyAssertsThatAllExpectationsHaveBeenSatisfied() {
30        listener.runTestIn(JUnit5TestThatDoesNotSatisfyExpectations.class);
31        listener.assertTestFailedWith(AssertionError.class);
32    }
33
34    @Test
35    public void testTheJUnit5TestRunnerLooksForTheMockeryInBaseClasses() {
36        listener.runTestIn(DerivedJUnit5TestThatDoesNotSatisfyExpectations.class);
37        listener.assertTestFailedWith(AssertionError.class);
38    }
39
40    @Test
41    public void testTheJUnit5TestRunnerReportsAHelpfulErrorIfTheMockeryIsNull() {
42        listener.runTestIn(JUnit5TestThatDoesNotCreateAMockery.class);
43        listener.assertTestFailedWith(org.junit.platform.commons.util.PreconditionViolationException.class);
44    }
45
46    // See issue JMOCK-156
47    @Test
48    public void testReportsMocksAreNotSatisfiedWhenExpectedExceptionIsThrown() {
49        listener.runTestIn(JUnit5TestThatThrowsExpectedException.class);
50        listener.assertTestFailedWith(AssertionError.class);
51    }
52
53    // See issue JMOCK-219
54    @Test
55    public void testTheJUnit5TestRunnerReportsIfNoMockeryIsFound() {
56        listener.runTestIn(JUnit5TestThatCreatesNoMockery.class);
57        listener.assertTestFailedWithInitializationError();
58    }
59
60    // See issue JMOCK-219
61    @Test
62    public void testTheJUnit4TestRunnerReportsIfMoreThanOneMockeryIsFound() {
63        listener.runTestIn(JUnit5TestThatCreatesTwoMockeries.class);
64        listener.assertTestFailedWithInitializationError();
65    }
66
67    @Test
68    public void testAutoInstantiatesMocks() {
69        listener.runTestIn(JUnit5TestThatAutoInstantiatesMocks.class);
70        listener.assertTestSucceeded();
71    }
72}
73
Full Screen
copy
1package org.jmock.junit5.acceptance;
2
3import org.jmock.junit5.testdata.jmock.acceptance.JUnit5WithRulesExamples;
4import org.junit.jupiter.api.Test;
5import org.junit.jupiter.api.extension.ExtensionConfigurationException;
6
7public class JUnit5WithRulesTestRunnerTests {
8    FailureRecordingTestExecutionListener listener = new FailureRecordingTestExecutionListener();
9    
10    @Test
11    public void testTheJUnit4TestRunnerReportsPassingTestsAsSuccessful() {
12        listener.runTestIn(JUnit5WithRulesExamples.SatisfiesExpectations.class);
13        listener.assertTestSucceeded();
14    }
15    
16    @Test
17    public void testTheJUnit4TestRunnerAutomaticallyAssertsThatAllExpectationsHaveBeenSatisfied() {
18        listener.runTestIn(JUnit5WithRulesExamples.DoesNotSatisfyExpectations.class);
19        listener.assertTestFailedWith(AssertionError.class);
20    }
21    
22    @Test
23    public void testTheJUnit4TestRunnerLooksForTheMockeryInBaseClasses() {
24        listener.runTestIn(JUnit5WithRulesExamples.DerivedAndDoesNotSatisfyExpectations.class);
25        listener.assertTestFailedWith(AssertionError.class);
26    }
27
28    
29    // See issue JMOCK-156
30    @Test
31    public void testReportsMocksAreNotSatisfiedWhenExpectedExceptionIsThrown() {
32        listener.runTestIn(JUnit5WithRulesExamples.ThrowsExpectedException.class);
33        listener.assertTestFailedWith(AssertionError.class);
34    }
35    
36    @Test
37    public void testFailsWhenMoreThanOneJMockContextField() {
38        listener.runTestIn(JUnit5WithRulesExamples.CreatesTwoMockeries.class);
39        listener.assertTestFailedWith(ExtensionConfigurationException.class);
40    }
41
42    @Test
43    public void testAutoInstantiatesMocks() {
44        listener.runTestIn(JUnit5WithRulesExamples.AutoInstantiatesMocks.class);
45        listener.assertTestSucceeded();
46    }
47}
48
Full Screen
copy
1package org.jmock.junit5.acceptance;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
5import static org.junit.jupiter.api.Assertions.fail;
6import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
7
8import java.util.function.Predicate;
9
10import org.junit.jupiter.api.extension.ExtensionConfigurationException;
11import org.junit.platform.engine.TestExecutionResult;
12import org.junit.platform.engine.TestExecutionResult.Status;
13import org.junit.platform.launcher.Launcher;
14import org.junit.platform.launcher.TestExecutionListener;
15import org.junit.platform.launcher.TestIdentifier;
16import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
17import org.junit.platform.launcher.core.LauncherFactory;
18
19import com.google.auto.service.AutoService;
20
21@AutoService(TestExecutionListener.class)
22public class FailureRecordingTestExecutionListener implements TestExecutionListener {
23
24    TestExecutionResult testExecutionResult;
25
26    @Override
27    public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
28        if (testIdentifier.isTest() /* ignore non test containers */) {
29            this.testExecutionResult = testExecutionResult;
30        }
31    }
32
33    public void assertTestSucceeded() {
34        if (testExecutionResult.getStatus().equals(Status.FAILED)) {
35            fail("test should have passed but reported failure: " + testExecutionResult.toString());
36        }
37    }
38
39    public void assertTestFailedWith(Class<? extends Throwable> exceptionType) {
40        assertEquals(Status.FAILED, testExecutionResult.getStatus(), "test should have failed");
41        Throwable cause = testExecutionResult.getThrowable().get();
42        assertTrue(exceptionType.isInstance(cause),
43                "should have failed with " + exceptionType.getName() + " but threw " + cause);
44    }
45
46    public void assertTestFailedWithInitializationError() {
47        assertEquals(Status.FAILED, testExecutionResult.getStatus(), "test should have failed");
48        assertTrue(
49                testExecutionResult.getThrowable()
50                        .filter(new Predicate<Throwable>() {
51                            @Override
52                            public boolean test(Throwable t) {
53                                return t.getClass().equals(ExtensionConfigurationException.class);
54                            }
55                        })
56                        .isPresent(),
57                testExecutionResult.toString());
58    }
59
60    public void runTestIn(Class<?> testClass) {
61        Launcher launcher = LauncherFactory.create();
62        launcher.registerTestExecutionListeners(this);
63        launcher.execute(LauncherDiscoveryRequestBuilder.request()
64                .selectors(selectClass(testClass))
65                .build());
66    }
67}
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 code on LambdaTest Cloud Grid

Execute automation tests with 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
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)