junit5单元测试

前言

接上一篇 https://idea360.cn/2021/04/13/junit4/ junit4单元测试

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.idea360</groupId>
<artifactId>junit-demo</artifactId>
<version>1.0.0</version>

<name>junit-demo</name>
<description>junit4 mock测试. 欢迎关注公众号【当我遇上你】</description>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>4.7.2</version>
</dependency>
</dependencies>
</project>

json文件

/src/test/resources/testData/simpleDocumentWithSubList.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"_id": {
"$oid": "50b8d63414f85401b9268b99"
},
"title": "XP by example",
"tags": [ "pair programming", "tdd", "agile"],
"innerList" : [
[1, 2, 3, 4],
[false , true],
[
{ "tagName": "pouet"},
{ "tagName": "paf"}
]
]
}

单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cn.idea360.junit.filter;

import cn.idea360.junit.delegate.DelegatingServletInputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Slf4j
public class LogFilterTest {

private StringWriter writer;

@BeforeAll
static void setup() {
log.info("@BeforeAll - executes once before all test methods in this class");
}

@BeforeEach
void init() {
log.info("@BeforeEach - executes before each test method in this class");
writer = new StringWriter();
}

@Test
public void logFilter() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);

String requestBody = "{\"msg\": \"Hello World\"}";

when(request.getHeader("blog")).thenReturn("idea360.cn");
when(request.getMethod()).thenReturn("POST");
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/junit-demo/sign"));
when(request.getContextPath()).thenReturn("/junit-demo");
when(request.getServletPath()).thenReturn("/sign");
when(request.getRequestURI()).thenReturn("/junit-demo/sign");
when(request.getCharacterEncoding()).thenReturn(StandardCharsets.UTF_8.name());
when(request.getContentType()).thenReturn("application/json");
when(request.getParameter("age")).thenReturn("17");
when(request.getQueryString()).thenReturn("name=admin");
when(request.getInputStream()).thenReturn(new DelegatingServletInputStream(new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8))));

when(response.getWriter()).thenReturn(new PrintWriter(writer));

LogFilter filter = new LogFilter();
filter.doFilter(request, response, filterChain);

Assertions.assertEquals("{\"msg\":\"验签失败\",\"code\":401}", writer.toString());
}

@Test
void jsonFile() throws IOException {
String jsonFile = "/testData/simpleDocumentWithSubList.json";
String json = IOUtils.toString(getClass().getResourceAsStream(jsonFile), Charset.defaultCharset());
Document document = Document.parse(json);
document.put("_id", new ObjectId(String.valueOf(document.get("_id"))));
log.info(String.valueOf(document));
}
}