在这里,我分享我的mockito MockStatic解决方案,它基于我在回答leokom解决方案时承诺的扩展。
那么,为什么Mockito选择了try-with-resources?嗯,只是因为他们想保持一艘整洁的船。毕竟这是很好的编程。Try-with-resources允许在保证调用close方法的情况下进行构造。但是在JUnit中,我们已经在BeforeEach和AfterEach中有了。并且可以使用实现BeforeEachCallback和AfterEachCallback的Extension轻松地将它们添加到每个测试类中。
理论到此为止。让我们做一个静态模拟
Instant.now()
我从一个注释开始,以便能够标记我的测试类中希望用作静态模拟的字段。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface StaticMock {
}
这允许我在我的测试类中为静态模拟创建一个字段,我可以很容易地在我的扩展类中找到它。
@StaticMock
private MockedStatic<Instant> staticInstantMock;
我将我创建的扩展添加到我的测试类。你有两个选择。
为此目的创建一个扩展,并将其添加到您也需要的MockitoExtension旁边的类中。
创建一个扩展,并从MockitoExtension继承。现在可以在测试类上替换MockitoExtension。
我用的是后者。
@ExtendWith({CompanyMockitoExtension.class})
class MyExtendedTestClass {
现在我们需要在调用static时为它返回一些东西:
@Mock
private Instant now;
staticInstantMock.when(Instant::now).thenReturn(now);
整个测试类:
@ExtendWith({CompanyMockitoExtension.class})
class MyExtendedTestClass {
@StaticMock
private MockedStatic<Instant> staticInstantMock;
@Mock
private Instant now;
@Test
void myTestMethod() {
staticInstantMock.when(Instant::now).thenReturn(now);
assertThat(Instant::now).isSameAs(now); // This would normally happen in the class you are testing...
}
}
现在让我们看一下Extension类。
import static org.mockito.Mockito.mockStatic;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
public class CompanyMockitoExtension extends MockitoExtension {
@Override
public void beforeEach(ExtensionContext context) {
super.beforeEach(context); // Don't forget to call the super!!
if (context.getTestInstance().isEmpty()) { // Just to be sure...
return;
}
// Get the unit test instance
Object testSubject = context.getTestInstance().get();
initializeStaticMocks(testSubject);
}
private void initializeStaticMocks(Object testSubject) {
// Find all fields that I want to static mock
List<Field> staticMockFields = ReflectionHelper.getFieldsWithAnnotation(testSubject, StaticMock.class);
staticMockFields.forEach(field -> initializeStaticMock(field, testSubject));
}
private void initializeStaticMock(Field field, Object testSubject) {
// Get the type of the static mock. It is within the generic MockedStatic<> class type.
Class<?> typeForStaticMock = (Class<?>) ReflectionHelper.getTypesForGeneric(field)[0];
try {
// Now set the field with the mockStatic method of Mockito.
field.setAccessible(true);
field.set(testSubject, mockStatic(typeForStaticMock));
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to instantiate Static Mock with type: " + typeForStaticMock.getName());
}
}
@Override
public void afterEach(ExtensionContext context) {
super.afterEach(context); // Again, do not forget to call the super.
if (context.getTestInstance().isEmpty()) {
return;
}
Object testSubject = context.getTestInstance().get();
closeStaticMocks(testSubject); // Close all static mocks.
}
private void closeStaticMocks(Object testSubject) {
// Again find all fields we annotated
List<Field> staticMockFields = ReflectionHelper.getFieldsWithAnnotation(testSubject, StaticMock.class);
staticMockFields.forEach(field -> closeStaticMock(field, testSubject));
}
private void closeStaticMock(Field field, Object testSubject) {
// Get the instance and simply call close.
MockedStatic<?> mockedStaticInstance = ReflectionHelper.getFieldInstance(field, testSubject, MockedStatic.class);
mockedStaticInstance.close();
}
}
关于这个扩展的好处是,你可以添加额外的嘲弄的东西。我在AfterEach中添加了对所有模拟没有更多交互的验证。这现在是自动的,当我们使用这个扩展。我还为构造模拟添加了与静态模拟相似的行为。
如您所见,我创建了自己的反射助手类。我知道有一些标准的反射助手类,它们可能更好。这是我的。
public class ReflectionHelper {
public static List<Field> getFieldsWithAnnotation(
Object testSubject,
Class<? extends Annotation> annotationType
) {
Class<?> testSubjectClass = testSubject.getClass();
return Arrays.stream(testSubjectClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(annotationType))
.collect(toUnmodifiableList());
}
public static List<Field> getCollectionFields(Object testSubject) {
Class<?> testSubjectClass = testSubject.getClass();
return Arrays.stream(testSubjectClass.getDeclaredFields())
.filter(field -> Collection.class.isAssignableFrom(field.getType()))
.collect(toUnmodifiableList());
}
@SuppressWarnings("unchecked")
public static <T> T getFieldInstance(Field field, Object testSubject, Class<T> type) {
return (T) getFieldInstance(field, testSubject);
}
public static Object getFieldInstance(Field field, Object testSubject) {
try {
boolean isStatic = isStatic(field.getModifiers());
Object context = isStatic ? null : testSubject;
field.setAccessible(true);
return field.get(context);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to get instance of field.");
}
}
public static Type[] getTypesForGeneric(Field field) {
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
return parameterizedType.getActualTypeArguments();
}
}