简体   繁体   English

如何创建仅适用于测试方法而不适用于整个测试类的JUnit5自定义扩展

[英]How to create JUnit5 custom extension that could be applied only on test methods and not on the entire test class

I want to create a JUnit5 extension that will resolve a test method parameters, implementing ParameterResolver : 我想创建一个JUnit5扩展来解析测试方法的参数,实现ParameterResolver

public class MyParameterResolver implements ParameterResolver {

    @Override
    public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        ...
    }

    @Override
    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        ...
    }
}

The extension is meant to use only on top of test methods: 该扩展仅可用于测试方法:

public class DemoTest {

    @Test
    @ExtendWith(MyParameterResolver.class)
    public void test(int x, int y) {
        ...
    }

    @Test
    @ExtendWith(MyParameterResolver.class)
    public void test2(int x, int y) {
        ...
    }
} 

How can I prevent it from being used on top of the test class (preferably at compile time if possible, but runtime would be good either)? 如何防止它在测试类的顶部使用(最好在编译时使用,但运行时也可以)?

//I don't want this to work
@ExtendWith(MyParameterResolver.class)
public class DemoTest {
    ...
} 

To check at compile time: 要在编译时检查:

If you look at @ExtendWith source code, you will see it is a composite annotation and there is a @Target({ElementType.TYPE, ElementType.METHOD}) . 如果查看@ExtendWith源代码,将会看到它是一个复合注释,并且有一个@Target({ElementType.TYPE, ElementType.METHOD}) If this was just @ElementType.METHOD , it will do exactly what you want and not permit @ExtendWith on a class. 如果这只是@ElementType.METHOD ,它将完全按照您想要的去做,而不允许在类上使用@ExtendWith

Changing the @ExtendWith however, is not a good option - you do not own it and you want to be able to use it everywhere :) 但是,更改@ExtendWith不是一个好选择-您不拥有它,并且希望能够在任何地方使用它:)

What you can do is create your own composite annotation: 可以做的是创建自己的复合注释:

@Target(value = {ElementType.METHOD})
@ExtendWith(MyParameterResolver.class)
public @interface MyParameterResolverAnnotation {
}

You can now use that instead of @ExtendWith(MyParameterResolver.class) and it will not compile if put on a class. 现在,您可以使用它代替@ExtendWith(MyParameterResolver.class),并且如果放在类上,它将无法编译。

I cannot think about another way to prevent this from happening at compile time. 我想不出另一种方法来防止这种情况在编译时发生。

At runtime/build time you have more options, like setting a custom checkstyle rule that will break the build in case it finds @ExtendWith(MyParameterResolver.class) on a class. 在运行时/构建时,您有更多选择,例如设置自定义checkstyle规则, @ExtendWith(MyParameterResolver.class)在类上发现@ExtendWith(MyParameterResolver.class)时中断构建。

You can even combine both of the approaches. 您甚至可以结合两种方法。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM