是否可以像我@Rule
在 JUnit4 中创建的那样创建自定义扩展?
public class MockMetadataServiceRule extends ExternalResource {
@Override
protected void before() throws Throwable {
//some setup
}
@Override
protected void after() {
// some teardown
}
}
然后,我可以使用JUnit5在测试类中执行此操作
@BeforeAll
public static void init() {
MetadataServiceFactory.setService(MockitoMockMetadataService.getMock());
}
@AfterAll
public static void teardown() {
MetadataServiceFactory.setService(null);
}
但我想你现在可以@ExtendWith(MyExtension.class)
吗?
编辑:我的扩展示例以防@nullpointer 链接消失。谢谢。
public class MockMetadataServiceExtension implements BeforeAllCallback, AfterAllCallback {
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
MetadataServiceFactory.setService(null);
}
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
MetadataServiceFactory.setService(MockitoMockMetadataService.getMock());
}
}