The first part, SpringBoot adds junit unit test
SpringBoot is very simple to use junit, let's take a look, first of all, here is the version of springboot2.0.4
One. Pom.xml file to open the springboot test package
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. Create a test class
1. For single file testing, just add @Test annotation, as shown in the figure:
2. MVC form call
Test class, need to add two annotations
@RunWith(SpringRunner.class) @SpringBootTest(classes={App.class})
Where App.class is the main program entry class, that is, the boot class of springboot
package com.qfx.test;
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 com.qfx.system.App;
import com.qfx.system.service.SysUserService;
(classes={App.class})
public class JunitTest {
<span class="hljs-meta">@Autowired</span>
SysUserService sysUserService;
<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printSysUserInfo</span><span class="hljs-params">()</span></span>{
String userStr = sysUserService.getSysUserAll();
System.out.println(userStr);
}
}
When testing, springboot will be started through App.class, let's take a look at the effect, as shown in the figure:
As for the Controller tests can directly start the project, to send the request to test the browser, if you really want to use junit test you can use to MockMvc be
The second part, Spring Boot's test library
Spring Boot provides many practical tools and annotations to help test the application, mainly including the following two modules.
-
spring-boot-test: core content that supports testing.
-
spring-boot-test-autoconfigure: supports automated configuration of tests.
Developed for use as long as spring-boot-starter-test
the initiator can be introduced into the test modules Spring Boot, but also introduce some like JUnit, AssertJ, Hamcrest
and other useful libraries, as shown below.
- JUnit: A standard class library for Java application unit testing.
- Spring Test & Spring Boot Test: Integrated test support for Spring Boot application functions.
- AssertJ: A lightweight assertion library.
- Hamcrest: An object matching library.
- Mockito: A Java Mock testing framework, default payment 1.x, can be modified to 2.x.
- JSONassert: An assertion library for JSON.
- JsonPath: a JSON operation class library.
The following is the dependency diagram of Maven.
These are some of the more commonly used test libraries provided by Spring Boot. If the above does not meet your needs, you can also add other libraries that are not available above.
Test the Spring Boot application
Add Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
<scope>test</scope>
</dependency>
1. To make a common class into a unit test class, just add @SpringBootTest and @RunWith (SpringRunner.class) two annotations to the class name.
2. Add @Test annotation to the test method.
If the test needs to make a REST call, you can @Autowire a TestRestTemplate.
@RunWith(SpringRunner.class)
@SpringBootTest
public class BBTestAA {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void testDemo() {
…
}
}
GET request test
@Test
public void get() throws Exception {
Map<String,String> multiValueMap = new HashMap<>();
multiValueMap.put("username","Java技术栈");
ActResult result = testRestTemplate.getForObject("/test/getUser?username={username}",ActResult.class,multiValueMap);
Assert.assertEquals(result.getCode(),0);
}
POST request test
@Test
public void post() throws Exception {
MultiValueMap multiValueMap = new LinkedMultiValueMap();
multiValueMap.add("username","Java技术栈");
ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class);
Assert.assertEquals(result.getCode(),0);
}
File upload test
@Test
public void upload() throws Exception {
Resource resource = new FileSystemResource("/home/javastack/test.jar");
MultiValueMap multiValueMap = new LinkedMultiValueMap();
multiValueMap.add("username","Java技术栈");
multiValueMap.add("files",resource);
ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class);
Assert.assertEquals(result.getCode(),0);
}
File download test
@Test
public void download() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.set("token","javastack");
HttpEntity formEntity = new HttpEntity(headers);
String[] urlVariables = new String[]{"admin"};
ResponseEntity<byte[]> response = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables);
if (response.getStatusCode() == HttpStatus.OK) {
Files.write(response.getBody(),new File("/home/javastack/test.jar"));
}
}
The second part of the above comes from: https://www.cnblogs.com/javastack/p/9150408.html