spring boot:如何构建自定义参数化测试切片

问题描述 投票:0回答:1

我想创建一个像@WebMvcTest一样的注释(它有很多参数,包括要测试的控制器),但还要加载其他配置@Import(...)

我看过文章https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4,但它描述了无参数注释。

如何“扩展”现有的测试切片?

java spring spring-boot spring-test spring-boot-test
1个回答
0
投票

可以这样导入控制器和其他配置:

@WebMvcTest(value = {MyController.class, MyConfig.class})

但是,如果您仍然想扩展@WebMvcTest注释,您可以创建一个组合的注释:

@Retention(RUNTIME)
@Target(TYPE)
@WebMvcTest
public @interface ExtendedMvcTest {

    @AliasFor(annotation = WebMvcTest.class, attribute = "value")
    Class<?>[] includeClasses() default {};
}

然后将其应用于测试:

@ExtendedMvcTest(includeClasses = {MyController.class, MyConfig.class})
class WebTests {}
© www.soinside.com 2019 - 2024. All rights reserved.