Skip to content

Commit

Permalink
Merge pull request #250 from olibye/junit5-integration-should-not-crash
Browse files Browse the repository at this point in the history
Fix #155
  • Loading branch information
olibye authored Mar 15, 2024
2 parents 9a08317 + b34ea98 commit f33ed4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
40 changes: 18 additions & 22 deletions jmock-junit5/src/main/java/org/jmock/junit5/JUnit5Mockery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Field> allFields = AllDeclaredFields.in(testCaseClass);
Expand All @@ -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();
}

Expand All @@ -79,37 +80,32 @@ private void fillInAutoMocks(final Object target, List<Field> 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<Field> findMockeryField(Class<?> testClass, ExtensionContext context) {
Optional<Field> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f33ed4a

Please sign in to comment.