Skip to content

Commit 7866542

Browse files
author
bluetata
committed
Add dynamic test logic
1 parent 6e1eeb2 commit 7866542

File tree

8 files changed

+230
-7
lines changed

8 files changed

+230
-7
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ This repository contains some examples of JUnit 5
88

99
## Sample Code Component contains
1010

11-
- [X] [Junit 5 Basic Test Sample](/src/main/java/com/example/junit5/test/basic)
12-
- [ ] [Junit 5 Dynamic Test Sample](/src/main/java/com/example/junit5/test/dynamic)
13-
- [X] [Junit 5 Spring Boot Test Sample](/src/main/java/com/example/junit5/test/springboot)
11+
- [X] [Junit 5 Basic Test Sample](/src/main/java/com/example/junit5/test/basic) (done)
12+
- [X] [Junit 5 Dynamic Test Sample](/src/main/java/com/example/junit5/test/dynamic) (done)
13+
- [X] [Junit 5 Spring Boot Test Sample](/src/main/java/com/example/junit5/test/springboot) (done)
1414

1515
## Test Steps
16-
> NOTE: You can choose clone this repo. or Download Zip file to local.
16+
> NOTE: You can choose *clone* this repo. or *Download Zip* file to local.
1717
1818
1. [Clone the repo](#1-clone-the-repo)
1919

@@ -67,7 +67,7 @@ $ mvn test
6767

6868
As the repo using *maven-surefire-report-plugin* plug, you can find the test report in **scr/target/surefire-reports** folder.
6969

70-
##Sample output
70+
## Sample output
7171

7272
> Spring Running
7373
@@ -86,7 +86,7 @@ As the repo using *maven-surefire-report-plugin* plug, you can find the test rep
8686
![](doc/source/images/test-result-report.jpg)
8787

8888

89-
# License
89+
## License
9090
[Apache 2.0](LICENSE)
9191

9292

doc/source/images/.gitkeep

Whitespace-only changes.

doc/source/images/coverage.jpg

312 Bytes
Loading

pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127
<version>1.2.0</version>
128128
<scope>test</scope>
129129
</dependency>
130+
<dependency>
131+
<groupId>org.junit.platform</groupId>
132+
<artifactId>junit-platform-commons</artifactId>
133+
<version>1.2.0</version>
134+
</dependency>
130135
<!-- end::tests[] -->
131136

132137
</dependencies>
@@ -155,7 +160,6 @@
155160
</pluginRepository>
156161
</pluginRepositories>
157162

158-
159163
<reporting>
160164
<plugins>
161165
<plugin>

src/main/java/com/example/junit5/test/dynamic/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.junit5.test.dynamic;
2+
3+
import org.junit.platform.commons.util.StringUtils;
4+
5+
/**
6+
* @author bluetata / [email protected]</br>
7+
* @version java-maven-junit5-example version(1.0)</br>
8+
* @date 09/06/18 15:44</br>
9+
* @since JDK 1.8</br>
10+
*/
11+
public class TranslatorEngine {
12+
13+
public String tranlate(String text) {
14+
if (StringUtils.isBlank(text)) {
15+
throw new IllegalArgumentException("Translated text must not be empty.");
16+
}
17+
if ("Hello".equalsIgnoreCase(text)) {
18+
return "你好";
19+
20+
} else if ("Yes".equalsIgnoreCase(text)) {
21+
return "是";
22+
23+
} else if ("No".equalsIgnoreCase(text)) {
24+
return "否";
25+
26+
} else if ("Goodbye".equalsIgnoreCase(text)) {
27+
return "再见";
28+
29+
} else if ("Good night".equalsIgnoreCase(text)) {
30+
return "晚安";
31+
32+
} else if ("Thank you".equalsIgnoreCase(text)) {
33+
return "谢谢";
34+
} else {
35+
return "Not found";
36+
}
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.example.junit5.test.dynamic;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.junit.jupiter.api.function.Executable;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
/**
13+
* 使用Junit 5 dynamic test 进行动态测试
14+
* @author bluetata / [email protected]</br>
15+
* @version java-maven-junit5-example version(1.0)</br>
16+
* @date 09/06/18 15:44</br>
17+
* @since JDK 1.8</br>
18+
*/
19+
public class TranslationEngineDynamicTest {
20+
21+
private TranslatorEngine translatorEngine;
22+
23+
@BeforeEach
24+
@DisplayName("Before run test case to init resource -> TranslatorEngine")
25+
public void init() {
26+
translatorEngine = new TranslatorEngine();
27+
}
28+
29+
public void translateDynamicTestsRaw() {
30+
31+
List<String> inPhrases = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
32+
List<String> outPhrases = new ArrayList<>(Arrays.asList("你好", "是", "否"));
33+
Collection<DynamicTest> dynamicTests = new ArrayList<>();
34+
35+
for (int i = 0; i < inPhrases.size(); i++) {
36+
37+
String phr = inPhrases.get(i);
38+
String outPhr = outPhrases.get(i);
39+
40+
// create an test execution
41+
Executable exec = () -> assertEquals(outPhr, translatorEngine.tranlate(phr));
42+
43+
// create a test display name
44+
String testName = "Test tranlate " + phr;
45+
// create dynamic test
46+
DynamicTest dTest = DynamicTest.dynamicTest(testName, exec);
47+
48+
// add the dynamic test to collection
49+
dynamicTests.add(dTest);
50+
}
51+
}
52+
53+
//
54+
//
55+
56+
/**
57+
* @TestFactory method must return a Stream, Collection, Iterable, or Iterator of DynamicTest instances
58+
* @TestFactory 方法强制限制返回实例为: Stream, Collection, Iterable, or Iterator of DynamicTest.
59+
*
60+
* @return Collection 集合
61+
*/
62+
@Disabled
63+
// @TestFactory
64+
public Collection<DynamicTest> translateDynamicTests() {
65+
66+
List<String> inPhrases =
67+
new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you"));
68+
List<String> outPhrases =
69+
new ArrayList<>(Arrays.asList("你好", "是", "否", "再见", "晚安", "谢谢"));
70+
71+
Collection<DynamicTest> dynamicTests = new ArrayList<>();
72+
73+
for (int i = 0; i < inPhrases.size(); i++) {
74+
75+
String phr = inPhrases.get(i);
76+
String outPhr = outPhrases.get(i);
77+
78+
// create an test execution
79+
Executable exec = () -> assertEquals(outPhr, translatorEngine.tranlate(phr));
80+
81+
// create a test display name
82+
String testName = "Test tranlate " + phr;
83+
// create dynamic test
84+
DynamicTest dTest = DynamicTest.dynamicTest(testName, exec);
85+
86+
// add the dynamic test to collection
87+
dynamicTests.add(dTest);
88+
}
89+
return dynamicTests;
90+
}
91+
92+
// 返回 Stream 流
93+
// @Disabled
94+
@TestFactory
95+
public Stream<DynamicTest> translateDynamicTestsFromStream() {
96+
97+
// set all test action to cover 100%
98+
List<String> inPhrases =
99+
new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you", "Other", " "));
100+
List<String> outPhrases =
101+
new ArrayList<>(Arrays.asList("你好", "是", "否", "再见", "晚安", "谢谢", "Not found", null));
102+
103+
return inPhrases.stream().map(phrs -> DynamicTest.dynamicTest("Test translate " + phrs, () -> {
104+
int idx = inPhrases.indexOf(phrs);
105+
assertEquals(outPhrases.get(idx), translatorEngine.tranlate(phrs));
106+
}));
107+
}
108+
109+
// 返回 Iterable 迭代器
110+
// @TestFactory
111+
public Iterable<DynamicTest> translateDynamicTestsFromIterate() {
112+
113+
List<String> inPhrases =
114+
new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you"));
115+
List<String> outPhrases =
116+
new ArrayList<>(Arrays.asList("你好", "是", "否", "再见", "晚安", "谢谢"));
117+
118+
return inPhrases.stream().map(phrs -> DynamicTest.dynamicTest("Test translate " + phrs, () -> {
119+
int idx = inPhrases.indexOf(phrs);
120+
assertEquals(outPhrases.get(idx), translatorEngine.tranlate(phrs));
121+
})).collect(Collectors.toList());
122+
}
123+
124+
// 返回 Iterator 迭代器
125+
@Disabled
126+
// @TestFactory
127+
public Iterator<DynamicTest> translateDynamicTestsFromIterator() {
128+
129+
List<String> inPhrases =
130+
new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you"));
131+
List<String> outPhrases =
132+
new ArrayList<>(Arrays.asList("你好", "是", "否", "再见", "晚安", "谢谢"));
133+
134+
return inPhrases.stream().map(phrs -> DynamicTest.dynamicTest("Test translate " + phrs, () -> {
135+
int idx = inPhrases.indexOf(phrs);
136+
assertEquals(outPhrases.get(idx), translatorEngine.tranlate(phrs));
137+
})).iterator();
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.junit5.test.dynamic;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
/**
10+
* 非动态测试示例, 需要对每个branch创建测试用例, 并进行测试
11+
* @author bluetata / [email protected]</br>
12+
* @version java-maven-junit5-example version(1.0)</br>
13+
* @date 09/06/18 15:44</br>
14+
* @since JDK 1.8</br>
15+
*/
16+
class TranslatorEngineTest {
17+
18+
private TranslatorEngine translatorEngine;
19+
@BeforeEach
20+
@DisplayName("Before run test case to init resource -> TranslatorEngine")
21+
void init() {
22+
translatorEngine = new TranslatorEngine();
23+
}
24+
25+
@Test
26+
public void testTranlsateHello() {
27+
assertEquals("你好", translatorEngine.tranlate("Hello"));
28+
}
29+
30+
@Test
31+
public void testTranlsateYes() {
32+
assertEquals("是", translatorEngine.tranlate("Yes"));
33+
}
34+
35+
@Test
36+
public void testTranlsateNo() {
37+
assertEquals("否", translatorEngine.tranlate("No"));
38+
}
39+
40+
//... other test case
41+
}

0 commit comments

Comments
 (0)