Overriding beans in Integration tests

后端 未结 7 1792
眼角桃花
眼角桃花 2020-12-23 09:16

For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the R

7条回答
  •  醉梦人生
    2020-12-23 09:42

    Getting a little deeper into it, see my second answer.

    I solved the Problem using

    @SpringBootTest(classes = {AppConfiguration.class, AppTestConfiguration.class})
    

    instead of

    @Import({ AppConfiguration.class, AppTestConfiguration.class });
    

    In my case the Test is not in the same package as the App. So I need to specify the AppConfiguration.class (or the App.class) explicit. If you use the same package in the test, than I guess you could just write

    @SpringBootTest(classes = AppTestConfiguration.class)
    

    instead of (not working)

    @Import(AppTestConfiguration.class );
    

    It is pretty wired to see that this is so different. Maybe some one can explain this. I could not find any good answers until now. You might think, @Import(...) is not picked up if @SpringBootTestsis present, but in the log the overriding bean shows up. But just the wrong way around.

    By the way, using @TestConfiguration instead @Configuration also makes no difference.

提交回复
热议问题