Prevent Application / CommandLineRunner classes from executing during JUnit testing

前端 未结 7 1479
既然无缘
既然无缘 2020-12-07 22:47

If in your TestCase class there is this annotations:

@SpringApplicationConfiguration(classes = {Application.class})

this will cause the

相关标签:
7条回答
  • 2020-12-07 22:58

    If you have a mocking framework installed (e.g. MockMVC) you can create a mock instance of the CommandLineRunner implementation, more or less disabling it:

    @MockBean private TextProcessor myProcessor;

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

    As mentioned in spring documentation http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html you can use @ContextConfiguration with a special initializer:

    ConfigFileApplicationContextInitializer is an ApplicationContextInitializer that can apply to your tests to load Spring Boot application.properties files. You can use this when you don’t need the full features provided by @SpringApplicationConfiguration.

    In this example anyComponent is initialized and properties are injected, but run(args) methods won't be executed. (Application.class is my main spring entry point)

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Application.class, 
                          initializers = ConfigFileApplicationContextInitializer.class)
    public class ExtractorTest {
        @Autowired
        AnyComponent anyComponent;
    
        @Test
        public void testAnyComponent() {
           anyComponent.anyMethod(anyArgument);
        }
    }
    
    0 讨论(0)
  • 2020-12-07 23:05

    You can define a test configuration in the same package as your application that looks exactly the same, except that it excludes beans implementing CommandLineRunner. The key here is @ComponentScan.excludeFilters:

    @Configuration
    @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
    @EnableAutoConfiguration
    public class TestApplicationConfiguration {
    }
    

    Then, just replace the configuration on your test:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = TestApplicationConfiguration.class)
    public class SomeApplicationTest {
        ...
    }
    

    No CommandLineRunner will be executed now, because they are not part of the configuration.

    0 讨论(0)
  • 2020-12-07 23:05

    I solve this by not implementing CommandLineRunner. Just get a bean from the context, and call a method on it, passing argv. That way you will get the same result, and the application won't start automatically when running the tests.

    0 讨论(0)
  • 2020-12-07 23:17

    Jan's solution can be achieved easier.

    In your test class, activate the "test" profile:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ActiveProfiles("test")
    public class MyFancyTest {}
    

    In your CommandLineRunner set the profile to NOT test:

    @Component
    @Profile("!test")
    public class JobCommandLineRunner implements CommandLineRunner {}
    

    Then you don't have to manually set the profile in the Application.

    0 讨论(0)
  • 2020-12-07 23:17

    I'm a bit late to the party, but a reasonable approach is to mark the bean with @ConditionalOnProperty, e.g.

    @ConditionalOnProperty(prefix = "job.autorun", name = "enabled", havingValue = "true", matchIfMissing = true)
    public CommandLineRunner myRunner() {...}
    

    The following annotation will then disable it in tests:

    @SpringBootTest(properties = {"job.autorun.enabled=false"})
    
    0 讨论(0)
提交回复
热议问题