How to exclude *AutoConfiguration classes in Spring Boot JUnit tests?

前端 未结 13 1009
名媛妹妹
名媛妹妹 2020-12-01 01:51

I tried:

@RunWith(SpringJUnit4ClassRunner.class)
@EnableAutoConfiguration(exclude=CrshAutoConfiguration.class)
@SpringApplicationConfiguration(classes = Appl         


        
相关标签:
13条回答
  • 2020-12-01 02:10

    Top answers don't point to an even simpler and more flexible solution.

    just place a

    @TestPropertySource(properties=
    {"spring.autoconfigure.exclude=comma.seperated.ClassNames,com.example.FooAutoConfiguration"})
    @SpringBootTest
    public class MySpringTest {...}
    

    annotation above your test class. This means other tests aren't affected by the current test's special case. If there is a configuration affecting most of your tests, then consider using the spring profile instead as the current top answer suggests.

    Thanks to @skirsch for encouraging me to upgrade this from a comment to an answer.

    0 讨论(0)
  • 2020-12-01 02:14

    got into same kind of problem, wasn't able to exclude main spring boot class during testing. Solved it using following approach.

    Instead of using @SpringBootApplication, use all three annotations which it contains and assign the name to @Configuration

    @Configuration("myApp")
    @EnableAutoConfiguration
    @ComponentScan
    public class MyApp { .. }
    

    In your test class define configuration with exactly same name:

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    // ugly hack how to exclude main configuration
    @Configuration("myApp")
    @SpringApplicationConfiguration(classes = MyTest.class)
    public class MyTest { ... }
    

    This should help. Would be nice to have some better way in place how to disable auto scanning for configuration annotations...

    0 讨论(0)
  • 2020-12-01 02:15

    I have struggled with a similar issue for one day... My Scenario:

    I have a SpringBoot application and I use applicationContext.xml in scr/main/resources to configure all my Spring Beans. For testing(integration testing) I use another applicationContext.xml in test/resources and things worked as I have expected: Spring/SpringBoot would override applicationContext.xml from scr/main/resources and would use the one for Testing which contained the beans configured for testing.

    However, just for one UnitTest I wanted yet another customization for the applicationContext.xml used in Testing, just for this Test I wanted to used some mockito beans, so I could mock and verify, and here started my one day head-ache!

    The problem is that Spring/SpringBoot doesn't not override the applicationContext.xml from scr/main/resources ONLY IF the file from test/resources HAS the SAME NAME. I tried for hours to use something like:

    @RunWith(SpringJUnit4ClassRunner.class)
    @OverrideAutoConfiguration(enabled=true)
    @ContextConfiguration({"classpath:applicationContext-test.xml"})
    

    it did not work, Spring was first loading the beans from applicationContext.xml in scr/main/resources

    My solution based on the answers here by @myroch and @Stuart:

    1. Define the main configuration of the application:

      @Configuration @ImportResource({"classpath:applicationContext.xml"}) public class MainAppConfig { }

    this is used in the application

    @SpringBootApplication
    @Import(MainAppConfig.class)
    public class SuppressionMain implements CommandLineRunner
    
    1. Define a TestConfiguration for the Test where you want to exclude the main configuration

      @ComponentScan( basePackages = "com.mypackage", excludeFilters = { @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = {MainAppConfig.class}) }) @EnableAutoConfiguration public class TestConfig { }

    By doing this, for this Test, Spring will not load applicationContext.xml and will load only the custom configuration specific for this Test.

    0 讨论(0)
  • 2020-12-01 02:16

    I had a similar problem but I came to a different solution that may help others. I used Spring Profiles to separate out test and app configuration classes.

    1. Create a TestConfig class with a specific profile and exclude any app configuration from component scan you wish here.

    2. In your test class set the profile to match the TestConfig and include it using the @ContextConfiguration annotation.

    For example:

    configuration:

    @Profile("test")
    @Configuration
    @EnableWebMvc
    @ComponentScan(
        basePackages="your.base.package",
        excludeFilters = {
                @Filter(type = ASSIGNABLE_TYPE,
                        value = {
                                ExcludedAppConfig1.class,
                                ExcludedAppConfig2.class
                })
        })
    public class TestConfig { ...}
    

    test:

    @ActiveProfiles("test")
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = TestConfig.class)
    @WebAppConfiguration
    public class SomeTest{ ... }
    
    0 讨论(0)
  • 2020-12-01 02:16

    If you're having this problem with Spring Boot 1.4.x and up, you might be able to use @OverrideAutoConfiguration(enabled=true) to solve the problem.

    Similar to what was asked/answered here https://stackoverflow.com/a/39253304/1410035

    0 讨论(0)
  • 2020-12-01 02:23

    Another simple way to exclude the auto configuration classes,

    Add below similar configuration to your application.yml file,

    ---
    spring:
      profiles: test
      autoconfigure.exclude: org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
    
    0 讨论(0)
提交回复
热议问题