Skip to content

Commit 1a6b857

Browse files
committed
Junit5修改
1 parent 9e50b6b commit 1a6b857

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

pom.xml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,30 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<junit.jupiter.version>5.4.2</junit.jupiter.version>
1516
</properties>
1617

1718
<dependencies>
18-
<dependency>
19-
<groupId>junit</groupId>
20-
<artifactId>junit</artifactId>
21-
<version>3.8.1</version>
22-
<scope>test</scope>
23-
</dependency>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter-engine</artifactId>
22+
<version>${junit.jupiter.version}</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-params</artifactId>
28+
<version>${junit.jupiter.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
2432
<dependency>
2533
<groupId>org.powermock</groupId>
2634
<artifactId>powermock-module-junit4</artifactId>
2735
<version>1.6.5</version>
2836
<scope>test</scope>
2937
</dependency>
38+
3039
<dependency>
3140
<groupId>org.powermock</groupId>
3241
<artifactId>powermock-api-mockito</artifactId>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.swust.software.whitebox;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.CsvFileSource;
12+
import org.junit.jupiter.params.provider.CsvSource;
13+
14+
import junit.framework.Assert;
15+
16+
@DisplayName("Junit5测试")
17+
class Exam1Test2 {
18+
private double DELTA = 1e-15;
19+
Exam1 testObject;
20+
@BeforeAll
21+
static void setUpBeforeClass() throws Exception {
22+
}
23+
24+
@BeforeEach
25+
void setUp() throws Exception {
26+
testObject = new Exam1();
27+
}
28+
29+
@Test
30+
@DisplayName("直接测试")
31+
void test() {
32+
int a, b, c;
33+
a = 2;
34+
b = 0;
35+
c = 3;
36+
double actx = testObject.DoWork(a, b, c);
37+
int expx = 2;
38+
Assertions.assertEquals(expx, actx, DELTA);
39+
}
40+
41+
@ParameterizedTest
42+
@DisplayName("参数化测试")
43+
@CsvSource({"1,2,3,4", "1,1,1,1", "3,4,5,6"})
44+
public void testFun1(int a,int b, int c, int expx) {
45+
double actx = testObject.DoWork(a, b, c);
46+
Assertions.assertEquals(expx, actx, DELTA);
47+
}
48+
49+
@ParameterizedTest
50+
@DisplayName("参数化文件测试")
51+
@CsvFileSource(resources = "/testdata.csv", numLinesToSkip = 1)
52+
public void testFun2(int a,int b, int c, int expx) {
53+
double actx = testObject.DoWork(a, b, c);
54+
Assertions.assertEquals(expx, actx, DELTA);
55+
}
56+
57+
}

src/test/resources/testdata.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a,b,c,exp
2+
1,2,3,5
3+
1,2,4,2
4+
3,4,5,6

0 commit comments

Comments
 (0)