How to use @ComponentScan together with test-specific ContextConfigurations in SpringJunit4TestRunner?

后端 未结 4 1897
心在旅途
心在旅途 2021-01-01 14:32

I am testing a Spring Boot application. I have several test classes, each of which needs a different set of mocked or otherwise customized beans.

Here is a sketch of

4条回答
  •  囚心锁ツ
    2021-01-01 15:24

    I would do a couple of things:

    1. Move your test classes into a different package to avoid @ComponentScaning them.
    2. In Component1TestWithFakeCommunication, change @SpringApplicationConfiguration(classes = MyApplication.class) to @SpringApplicationConfiguration(classes = {MyApplication.class, Component1TestWithFakeCommunication.ContextConfiguration.class})

    That should give Spring sufficient information to mock up the test beans, but it should prevent the runtime ApplicationContext from noticing your test beans as well.

提交回复
热议问题