# 第一个 JUnit 单元测试
# POM
`pom.xml` 文件如下:
<?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>com.cmcc</groupId>
<artifactId>hello-spring</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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
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
主要增加了 junit:junit
依赖
# 创建测试类
在测试包下 src/main/test
创建一个名为 MyTest
的测试类,代码如下:
package com.cmcc.hello.spring.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MyTest {
/**
* 执行测试方法前执行
*/
@Before
public void before() {
System.out.println("执行 before() 方法");
}
/**
* 执行测试方法后执行
*/
@After
public void after() {
System.out.println("执行 after() 方法");
}
@Test
public void testSayHi() {
System.out.println("Hi Log4j");
}
@Test
public void testSayHello() {
System.out.println("Hello Log4j");
}
}
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
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