How to exclude some bean injection from Spring Boot Test?

question

when testing Controller, you don"t want the test method to go to the interceptor. After Baidu, the method used is to add @ ComponentScan"s @ excludeFilters annotation. But this line of code only works on the main entry of the program, and there is no response if it is added to the base class of Test. I want to ignore the automatic injection of Spring during testing and not affect the behavior of the main program. Thank you

this is the entrance to the startup program

/**
 * Spring could web
 */
@Configuration//
@EnableAutoConfiguration//
@EnableFeignClients(basePackages = {"com.konyo.service.client", "com.konyo.activiticommon.client", "com.konyo.teleport.common.inteceptor"})
@MapperScan(value = {"com.konyo.service.dao", "com.konyo.activiticommon.mapper"})
@ComponentScans(value = {
    @ComponentScan(value = {"com.konyo.teleport", "com.konyo.service", "com.konyo.activiticommon"})//
})
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class JsGfrcServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(JsGfrcServiceApplication.class, args);
    }
}

this is my test base class


/**
 * Created by xx on 2018/07/12.
 * Contract 
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = JsGfrcServiceApplication.class)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@DirtiesContext
// 
@ComponentScan(value = {"com.konyo.service", "com.konyo.activiticommon"},
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
                classes = {RedisConfig.class, SwaggerConfig.class,
                        TxManagerHttpRequestServiceImpl.class, TxManagerTxUrlServiceImpl.class,
                        InitDictionaryMap.class}))
@Transactional

public class MockMvcContractTest {

    static {
        System.setProperty("eureka.client.enabled", "false");
        System.setProperty("spring.cloud.config.failFast", "false");
        System.setProperty("spring.cloud.config.discovery.enabled", "false");
    }

    protected String packageGetParams(String paramName, List<String> values, String uri) {
        StringBuilder builder = new StringBuilder(uri);
        if (values == null || values.size() == 0) {
            return uri;
        }
        boolean contains = uri.contains("?");
        if (!contains) {
            builder.append("?");
        }
        for (String value : values) {
            builder.append(paramName).append("=").append(value).append("&");
        }
        builder.delete(builder.length() - 1, builder.length());
        return builder.toString();
    }

    @Autowired
    MockMvc mockMvc;

    @Before
    public void setup() {
        RestAssuredMockMvc.mockMvc(mockMvc);
    }

}
Apr.01,2021

answer from stackoverflow

 

@ SpringBootTest (webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = JsGfrcServiceApplication.class)

this classes attribute is wrong. Change it to your test class

Menu