piotrek
piotrek

Reputation: 14550

spring boot: how to build custom parameterised test slice

i would like to create an annotation that works like @WebMvcTest (which have a lot of paramters, including controllers to test) but also loads additional configuration @Import(...).

i've seen the post https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4 but it describes parameterless annotation.

How can I 'extend' the existing test slice?

Upvotes: 0

Views: 514

Answers (1)

aleshka-batman
aleshka-batman

Reputation: 198

Controller and additional configuration can be imported like this:

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

But if you still want to extend @WebMvcTest annotation, you can create a composed annotation:

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

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

And then apply it to a test:

@ExtendedMvcTest(includeClasses = {MyController.class, MyConfig.class})
class WebTests {}

Upvotes: 2

Related Questions