In Spring Boot, use Command Line Runner You can develop applications that run from the command line.
But in an application that uses CommandLineRunner When you run a test in JUnit, not only the test code but also the processing of CommandLineRunner itself is executed.
I felt a little crazy, so I wanted to avoid it somehow.
Execution code
SampleApplication.java
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@Override
public void run(String... arg) {
SampleOutput sample = new SampleOutput();
System.out.println(sample.sayTest());
System.out.println("!!!Sample CommandLineRunner Start!!!");
}
}
SampleOutput.java
package com.example.demo;
public class SampleOutput {
public String sayTest() {
return "test!!";
}
}
Test code
SampleOutputTest
package com.example.demo;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleOutputTest {
@Test
public void testTest() {
SampleOutput sample = new SampleOutput();
assertEquals("test!!", sample.sayTest());
}
}
The result of running JUnit is as follows.
Execution result
07:42:33.780 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.demo.SampleOutputTest]
abridgement
2018-06-13 07:42:34.458 INFO 6564 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2df9b86: startup date [Wed Jun 13 07:42:34 JST 2018]; root of context hierarchy
2018-06-13 07:42:35.128 INFO 6564 --- [ main] com.example.demo.SampleOutputTest : Started SampleOutputTest in 0.972 seconds (JVM running for 1.867)
test!!
!!!Sample CommandLineRunner Start!!!
2018-06-13 07:42:35.190 INFO 6564 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2df9b86: startup date [Wed Jun 13 07:42:34 JST 2018]; root of context hierarchy
When I run JUnit, it outputs with CommandLineRunner
test!!
!!!Sample CommandLineRunner Start!!!
Is output. I want only the test code to work, but even CommandLineRunner has been executed.
I don't want to run CommandLineRunner when running JUnit, so I'll look for a way to do it. Try it referring to the following. https://stackoverflow.com/questions/29344313/prevent-application-commandlinerunner-classes-from-executing-during-junit-test
This method is omitted because there are easy-to-understand articles on stackoverflow and Qiita. https://qiita.com/ftsan/items/80b00f8fb8129ecbdf97
However, I wonder if there is a different method I tried to see if there was another way that I could just fix the test code.
In the test code SampleOutputTest.java
Annotate SpringBootTest
ContextConfiguration(classes = SampleApplication.class, initializers = ConfigFileApplicationContextInitializer.class) Modify to
annotation.
SampleOutputTest.java
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SampleApplication.class,
initializers = ConfigFileApplicationContextInitializer.class)
public class SampleOutputTest {
Now when you run the test, you can test it without running CommandLineRunner.
The classes = SampleApplication.class
part specifies the class where the main method is located.
Execution result
07:44:49.301 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.demo.SampleOutputTest]
Omitted below
Now you can test without running CommandLineRunner with just the modification of the test code.
Does it mean that SpringBootTest
is loading various settings, but ContextConfiguration
does not load all the settings?
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
If you read this document carefully, you can find it at 43.4 Test Utilities.
It was written that you should use this if you do not need to use all the functions of SpringBootTest
.
@ContextConfiguration(classes = Config.class,
initializers = ConfigFileApplicationContextInitializer.class)
Recommended Posts