Junit is a well-known Java testing framework. By integrating Junit + SpringBoot Test, you can use SpringBoot’s dependency injection and other SpringBoot features in Test programs.
Rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
Copy the code
The test Service
Location: SRC/main/Java/com/example/service/impl HelloServiceImpl. Java
package com.example.service.impl;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String helloTo(String name) {
// Write a Bug on purpose
if ("C++".equals(name))
return "Don't hello to C++";
return StringUtils.hasText(name) ? "HELLO " + name : "HELLO WORLD"; }}Copy the code
To test this code, Java’s general way is to create the required resources and test them, but then you can’t use SpringBoot’s various features. If there are too many dependencies, or there are too many configurations to read, it’s a nightmare:
private HelloService helloService;
@Before
public void setUp(a) {
helloService = new HelloService();
}
@Test
public void helloTo(a) {
assertEquals("HELLO WORLD", helloService.helloTo(""));
assertEquals("HELLO WORLD", helloService.helloTo("WORLD"));
assertEquals("HELLO JAVA", helloService.helloTo("JAVA"));
assertEquals("HELLO C++", helloService.helloTo("C++")); / / error
}
Copy the code
Method 1: Annotate SpringBoot
Location: the SRC/test/Java/com/example/service/impl HelloServiceImplTest. Java
Key points:
- Using the Junit SpringBoot Test framework:
@RunWith(SpringRunner.class)
- Specify startup entry:
@springboottest (classes = [SpringBoot program entry class])
At the end of the test, the SpringBoot context is automatically closed.
package com.example.service.impl;
import com.example.SpringBootTestApplication;
import com.example.service.HelloService;
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;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootTestApplication.class)
public class HelloServiceImplTest {
@Autowired
private HelloService helloService;
@Test
public void helloTo(a) {
assertEquals("HELLO WORLD", helloService.helloTo(""));
assertEquals("HELLO WORLD", helloService.helloTo("WORLD"));
assertEquals("HELLO JAVA", helloService.helloTo("JAVA"));
assertEquals("HELLO C++", helloService.helloTo("C++")); / / error}}Copy the code
Method 2: Explicitly start SpringBoot
It is also possible to use all of SpringBoot’s features to test by starting the SpringBoot program with the SpringApplication:: Run method, but it is a little more difficult. And SpringBootContext must be closed manually after the test.
package com.example.service.impl;
import com.example.SpringBootTestApplication;
import com.example.service.HelloService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import static org.junit.Assert.assertEquals;
public class HelloServiceImplTest2 {
private ConfigurableApplicationContext context;
private HelloService helloService;
@Before
public void setUp(a) {
context = SpringApplication.run(SpringBootTestApplication.class);
// Get beans explicitly, depending on SpringBoot to inject them automatically
helloService = context.getBean(HelloService.class);
}
@After
public void tearDown(a) {
// Close the SpringBoot program
context.close();
}
@Test
public void helloTo(a) {
assertEquals("HELLO WORLD", helloService.helloTo(""));
assertEquals("HELLO WORLD", helloService.helloTo("WORLD"));
assertEquals("HELLO JAVA", helloService.helloTo("JAVA"));
assertEquals("HELLO C++", helloService.helloTo("C++")); / / error}}Copy the code