From b34ea98672b6833d302315f1efa1682c1310719a Mon Sep 17 00:00:00 2001 From: oliverbye Date: Wed, 13 Mar 2024 09:17:04 +0000 Subject: [PATCH] Fix #155 --- .../java/org/jmock/junit5/JUnit5Mockery.java | 40 +++++++++---------- .../acceptance/JUnit5TestRunnerTests.java | 6 +-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/jmock-junit5/src/main/java/org/jmock/junit5/JUnit5Mockery.java b/jmock-junit5/src/main/java/org/jmock/junit5/JUnit5Mockery.java index f5880a541..355206751 100644 --- a/jmock-junit5/src/main/java/org/jmock/junit5/JUnit5Mockery.java +++ b/jmock-junit5/src/main/java/org/jmock/junit5/JUnit5Mockery.java @@ -2,6 +2,7 @@ import java.lang.reflect.Field; import java.util.List; +import java.util.Optional; import org.jmock.Mockery; import org.jmock.auto.internal.Mockomatic; @@ -59,7 +60,7 @@ public JUnit5Mockery() { } @Override - public void beforeEach(ExtensionContext context) throws Exception { + public void beforeEach(ExtensionContext context) { if (context.getTestClass().isPresent()) { Class testCaseClass = context.getTestClass().get(); List allFields = AllDeclaredFields.in(testCaseClass); @@ -70,7 +71,7 @@ public void beforeEach(ExtensionContext context) throws Exception { } @Override - public void afterEach(ExtensionContext context) throws Exception { + public void afterEach(ExtensionContext context) { assertIsSatisfied(); } @@ -79,37 +80,32 @@ private void fillInAutoMocks(final Object target, List allFields) { } private static void checkMockery(ExtensionContext context, Class testCaseClass) { - Field mockeryField = findMockeryField(testCaseClass, context); - try { - // private extension fields are not called - // field will at least be default scope if we're called. - mockeryField.setAccessible(true); - if(mockeryField.get(context.getRequiredTestInstance()) == null) { - throw new IllegalStateException("JUnit5Mockery field should not be null"); + findMockeryField(testCaseClass, context).ifPresent(mockeryField -> { + try { + // private extension fields are not called + // field will at least be default scope if we're called. + mockeryField.setAccessible(true); + if (mockeryField.get(context.getRequiredTestInstance()) == null) { + throw new IllegalStateException("JUnit5Mockery field should not be null"); + } + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new ExtensionConfigurationException("Could not check the mockery", e); } - } catch (IllegalArgumentException e) { - throw new ExtensionConfigurationException("Could not check the mockery", e); - } catch (IllegalAccessException e) { - throw new ExtensionConfigurationException("Could not check the mockery", e); - } + }); } - private static Field findMockeryField(Class testClass, ExtensionContext context) { - Field mockeryField = null; + private static Optional findMockeryField(Class testClass, ExtensionContext context) { + Optional mockeryField = Optional.empty(); for (Field field : AllDeclaredFields.in(testClass)) { if (Mockery.class.isAssignableFrom(field.getType())) { - if (mockeryField != null) { + if (mockeryField.isPresent()) { throw new ExtensionConfigurationException("more than one Mockery found in test class " + testClass); } - mockeryField = field; + mockeryField = Optional.of(field); } } - if (mockeryField == null) { - throw new ExtensionConfigurationException("no Mockery found in test class " + testClass); - } - return mockeryField; } } diff --git a/jmock-junit5/src/test/java/org/jmock/junit5/acceptance/JUnit5TestRunnerTests.java b/jmock-junit5/src/test/java/org/jmock/junit5/acceptance/JUnit5TestRunnerTests.java index 05c8fb199..ed13e0811 100644 --- a/jmock-junit5/src/test/java/org/jmock/junit5/acceptance/JUnit5TestRunnerTests.java +++ b/jmock-junit5/src/test/java/org/jmock/junit5/acceptance/JUnit5TestRunnerTests.java @@ -50,11 +50,11 @@ public void testReportsMocksAreNotSatisfiedWhenExpectedExceptionIsThrown() { listener.assertTestFailedWith(AssertionError.class); } - // See issue JMOCK-219 + // See issue https://github.com/jmock-developers/jmock-library/issues/155 @Test - public void testTheJUnit5TestRunnerReportsIfNoMockeryIsFound() { + public void testTheJUnit5TestRunnerIgnoreIfNoMockeryIsFound() { listener.runTestIn(JUnit5TestThatCreatesNoMockery.class); - listener.assertTestFailedWithInitializationError(); + listener.assertTestSucceeded(); } // See issue JMOCK-219