在使用jUnit5的Spring Boot应用程序中执行许多测试之前,我需要做一个验证.基本上,我需要判断我的测试是否有超类(因为我们在那里预加载了上下文).我不想编辑每个其他测试,做一些允许我在运行每个测试之前进行判断的事情.我做了这样的事情:
public class InheritClassTestCheckExtension implements TestTemplateInvocationContextProvider, BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Class<?> superClass = context.getRequiredTestClass().getSuperclass();
String listClasses = "";
if (superClass doesnt exists or is object, throw a exception) {
throw new Exception("Need to inherit from class A,B or C");
} else {
Continue to the test's
}
}
我在我的junit-platform.properties中有这个代码
junit.jupiter.extensions.autodetection.enabled=true
junit.jupiter.extensions.autodetection.names=com.my.package.InheritClassTestCheckExtension
但这并不起作用(它甚至在每次之前都没有进入方法).我挡对了路吗?或者我可以用什么来做这件事?