Java Spring Boot Test: How to exclude java configuration class from test context

前端 未结 5 773
后悔当初
后悔当初 2021-01-17 07:41

I have a Java web app with spring boot

When run test I need to exclude some Java config files:

Test config (need to include when test run):

@         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 08:30

    The easiest way that I use -

    1. Put @ConditionalOnProperty in my main source code.
    2. Then define a test bean and add it as a part of the test configuration. Either import that configuration in the main test class or any other way. This is only needed when you need to register a test bean. If you do not need that bean then go to next step.
    3. Add a property in application.properties under src/test/resources to disable the use of configuration class that is present in the main folder.

    Voila!

提交回复
热议问题