Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Application / CommandLineRunner classes from executing during JUnit testing

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:

  1. to remove the CommandLineRunner interface from my Application class
  2. to have a different context for testing

Both this solution requires lot of coding. Do you have a more convenient solution?

like image 582
Gorgia666 Avatar asked Mar 30 '15 10:03

Gorgia666


People also ask

What is the difference between application runner and command line runner?

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.

What is the use of ApplicationRunner in spring boot?

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.

What is @SpringBootTest?

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.

What is spring boot CommandLineRunner?

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.


2 Answers

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.

like image 119
Michael P. Avatar answered Oct 13 '22 00:10

Michael P.


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);     } } 
like image 34
jmgonet Avatar answered Oct 12 '22 23:10

jmgonet