If in your TestCase class there is this annotations:
@SpringApplicationConfiguration(classes = {Application.class})
this will cause the Application.class
, implementing the CommandLineRunner
interface, to run the required method
public void run(String... args) throws Exception
I still think this is, mostly, a not wanted behaviour, since in your test environment you may not want to launch the entire application.
I have in mind two solution to circumvent this problem:
CommandLineRunner
interface from my Application
classBoth this solution requires lot of coding. Do you have a more convenient solution?
The difference between CommandLineRunner and ApplicationRunner is that the run() method of CommandLineRunner accepts array of String as an argument and run() method of ApplicationRunner accepts spring ApplicationArguments as an argument.
Application Runner and Command Line Runner interfaces lets you to execute the code after the Spring Boot application is started. You can use these interfaces to perform any actions immediately after the application has started.
The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.
CommandLineRunner is a simple Spring Boot interface with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded.
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.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With