| 1 |
package org.jmock.junit5; |
| 2 |
|
| 3 |
import java.lang.reflect.Field; |
| 4 |
import java.util.List; |
| 5 |
|
| 6 |
import org.jmock.Mockery; |
| 7 |
import org.jmock.auto.internal.Mockomatic; |
| 8 |
import org.jmock.internal.AllDeclaredFields; |
| 9 |
import org.jmock.lib.AssertionErrorTranslator; |
| 10 |
import org.junit.jupiter.api.extension.AfterEachCallback; |
| 11 |
import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 12 |
import org.junit.jupiter.api.extension.Extension; |
| 13 |
import org.junit.jupiter.api.extension.ExtensionConfigurationException; |
| 14 |
import org.junit.jupiter.api.extension.ExtensionContext; |
| 15 |
|
| 16 |
import com.google.auto.service.AutoService; |
| 17 |
|
| 18 |
/** |
| 19 |
* A <code>JUnit5Mockery</code> is a JUnit Extension that manages JMock |
| 20 |
* expectations and allowances, and asserts that expectations have been met |
| 21 |
* after each test has finished. To use it, add a (non-private) field to the test class |
| 22 |
* For example, |
| 23 |
* |
| 24 |
* <pre> |
| 25 |
* public class ATestWithSatisfiedExpectations { |
| 26 |
* @RegisterExtension |
| 27 |
* final JUnitRuleMockery context = new JUnitRuleMockery(); |
| 28 |
* |
| 29 |
* @Mock |
| 30 |
* private final Runnable runnable; |
| 31 |
* |
| 32 |
* @Test |
| 33 |
* public void doesSatisfyExpectations() { |
| 34 |
* context.checking(new Expectations() { |
| 35 |
* { |
| 36 |
* oneOf(runnable).run(); |
| 37 |
* } |
| 38 |
* }); |
| 39 |
* |
| 40 |
* runnable.run(); |
| 41 |
* } |
| 42 |
* } |
| 43 |
* </pre> |
| 44 |
* |
| 45 |
* Note that the Rule field must be declared public and as a |
| 46 |
* <code>JUnitRuleMockery</code> (not a <code>Mockery</code>) for JUnit to |
| 47 |
* recognise it, as it's checked statically. |
| 48 |
* |
| 49 |
* @author olibye |
| 50 |
*/ |
| 51 |
@AutoService(Extension.class) |
| 52 |
public class JUnit5Mockery extends Mockery |
| 53 |
implements Extension, BeforeEachCallback, AfterEachCallback { |
| 54 |
|
| 55 |
private final Mockomatic mockomatic = new Mockomatic(this); |
| 56 |
|
| 57 |
public JUnit5Mockery() { |
| 58 |
setExpectationErrorTranslator(AssertionErrorTranslator.INSTANCE); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void beforeEach(ExtensionContext context) throws Exception { |
| 63 |
if (context.getTestClass().isPresent()) { |
| 64 |
Class<?> testCaseClass = context.getTestClass().get(); |
| 65 |
List<Field> allFields = AllDeclaredFields.in(testCaseClass); |
| 66 |
fillInAutoMocks(context.getRequiredTestInstance(), allFields); |
| 67 |
|
| 68 |
checkMockery(context, testCaseClass); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void afterEach(ExtensionContext context) throws Exception { |
| 74 |
assertIsSatisfied(); |
| 75 |
} |
| 76 |
|
| 77 |
private void fillInAutoMocks(final Object target, List<Field> allFields) { |
| 78 |
mockomatic.fillIn(target, allFields); |
| 79 |
} |
| 80 |
|
| 81 |
private static void checkMockery(ExtensionContext context, Class<?> testCaseClass) { |
| 82 |
Field mockeryField = findMockeryField(testCaseClass, context); |
| 83 |
try { |
| 84 |
// private extension fields are not called |
| 85 |
// field will at least be default scope if we're called. |
| 86 |
mockeryField.setAccessible(true); |
| 87 |
if(mockeryField.get(context.getRequiredTestInstance()) == null) { |
| 88 |
throw new IllegalStateException("JUnit5Mockery field should not be null"); |
| 89 |
} |
| 90 |
} catch (IllegalArgumentException e) { |
| 91 |
throw new ExtensionConfigurationException("Could not check the mockery", e); |
| 92 |
} catch (IllegalAccessException e) { |
| 93 |
throw new ExtensionConfigurationException("Could not check the mockery", e); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private static Field findMockeryField(Class<?> testClass, ExtensionContext context) { |
| 98 |
Field mockeryField = null; |
| 99 |
|
| 100 |
for (Field field : AllDeclaredFields.in(testClass)) { |
| 101 |
if (Mockery.class.isAssignableFrom(field.getType())) { |
| 102 |
if (mockeryField != null) { |
| 103 |
throw new ExtensionConfigurationException("more than one Mockery found in test class " + testClass); |
| 104 |
} |
| 105 |
mockeryField = field; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (mockeryField == null) { |
| 110 |
throw new ExtensionConfigurationException("no Mockery found in test class " + testClass); |
| 111 |
} |
| 112 |
|
| 113 |
return mockeryField; |
| 114 |
} |
| 115 |
} |