Why are the @Configuration classes not being run in Spring Boot tests?

Viewed 6496

I have a Spring Boot @Configuration class that's located at com.app.config and a controller located at com.app.controller and my test (in the tests directory) is at com.app.controller. When I run it, the configuration class is never used.

package com.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@Configuration
@EnableWebMvc
@ComponentScan("com.app")
public class ValidationConfig {

    @Bean
    public Validator validator() {

        //The breakpoint here is never called!
        return new LocalValidatorFactoryBean();
    }
}

Test class:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.validation.Validator;

@RunWith(SpringRunner.class)
@ComponentScan({"com.app","com.app.config"})
public class TestAumController {

    //...elided...

    @Autowired
    private Validator validator;

    @Test
    public void testController() throws Exception {
        //..edlided...
    }
}
2 Answers
Related