6

I am writing junits for a rest controller. I want only minimal context to be loaded which is related to the controller and I think that is how @WebMvcTest loads the context. But the junit is loading complete Spring context and it is failing as some of the beans can not be created.

I have searched and read through many questions on stackoverflow and none of the solutions worked to exclude specific configuration. How can I load a minimal context when writing junits for controllers? Or is there any way to exclude some configuration classes? I am using Spring-boot 2.2.2.RELEASE, Java 8 and Junit 4.

Junit (where I have tried to exclude loading of some beans and configurations but it doesn't work):

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = OrderController.class, excludeFilters = {@Filter(classes = Configuration.class), @Filter(type = FilterType.REGEX, pattern = "com\\.foo\\..*")})
@TestPropertySource(properties = { "spring.cloud.config.server.bootstrap:false" })
public class OrderControllerTest {

    @MockBean
    private OrderService orderService;


    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() throws Exception {
        mockMvc.perform(get("/internal/order/123"));
        // Further code to verify the response
    }

}

Controller

@Slf4j
@Validated
@RestController
@RequestMapping("/internal")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping(value = "/order/{orderId}", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<RegularContributionOrder> retrieveRegularContributionOrder(@NotNull @PathVariable("orderId") String orderId)
            throws OrderNotFoundException {

        RegularContributionOrder order = orderService.retrieve(orderId);

        return new ResponseEntity<RegularContributionOrder>(order, HttpStatus.OK);
    }
}

Configuration class which I want to exclude from context loading

@Slf4j
@Configuration
@EnableAspectJAutoProxy
@ImportResource({ "classpath:spring/service-config-one.xml", "classpath:spring/service-config-two.xml" })
public class OrderServiceConfig {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:resourcebundles/error-messages.properties");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        return mapper;
    }
}

Spring boot main class:

@EnableJms
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "com.foo.services", "com.bar" })
@SpringBootApplication(exclude = { SomeConfiguration.class})
public class BootApplication extends SpringBootServletInitializer {

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(BootApplication .class);
    }

    public static void main(final String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        SpringApplication.run(BootApplication.class, args);
    }
}
2
  • Can you share your main class that is annotated with @SpringBootApplication as well please? Feb 17, 2020 at 7:09
  • @AndyWilkinson Thanks for the comment. Question updated.
    – Smile
    Feb 17, 2020 at 7:13

2 Answers 2

11

Your use of @ComponentScan has disabled the filters that are used by @WebMvcTest to limit the types of components that are found by scanning.

You should remove @ComponentScan from your main application class and use the basePackages attribute on @SpringBootApplication instead.

You should also move @EnableJms and @EnableAspectJAutoProxy to a separate @Configuration class so that they are not enabled when using @WebMvcTest. Alternatively, you may be able to remove them entirely as they are covered by Spring Boot’s auto-configuration.

6
  • 1
    works amazing. It should be the parameter "basePackages" in @SpringBootApplication instead of "backPackages". Probably a small typo
    – Prometheus
    Apr 6, 2021 at 1:18
  • 1
    Thanks, @Prometheus. I've fixed the typo. Apr 6, 2021 at 12:53
  • @AndyWilkinson how could we use "@SpringBootApplication" "basePackages" and exclude some class without using "@ComponentScan" excludeFilter since the exclude attribute in SpringBootApplication annotation is for exclude auto configuration class only.
    – Chi Dov
    Jul 18, 2022 at 20:08
  • @ChiDov You'd have to copy the @ComponentScan from @SpringBootApplication and configure additional exclude filters. Jul 19, 2022 at 9:40
  • 1
    @ChiDov That's what I said to copy the declaration from @SpringBootApplication including the exclude filters that it declares. You can then add additional filters of your own. Please ask a separate question if you need further guidance. Comments aren't really the right place for this. Jul 19, 2022 at 16:46
2

When your test classes located in test folder and run with "test" profile then your real configuration class will be excluded. Here is an example of how I configure spring boot test.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IntegrationApplication.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class MyControllerTest {

=================================

@SpringBootApplication
public class IntegrationApplication {

public static void main(String[] args) {
    SpringApplication.run(IntegrationApplication.class, args);
}
3
  • What is IntegrationApplication? Spring boot main class?
    – Smile
    Feb 17, 2020 at 7:22
  • Yes. This class for tests only. There is another spring boot main which is "real". Also you can profile your configuration class, like "@Profile("!test")" to exclude it from test context.
    – Cayman
    Feb 17, 2020 at 7:26
  • Thank you for you answer. @Andy Wilkinson 's answer worked for me.
    – Smile
    Feb 17, 2020 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.