Skip to content

Commit a0128a6

Browse files
committed
SpringBoot Junit测试
1 parent 0bfcbcb commit a0128a6

File tree

7 files changed

+158
-1
lines changed

7 files changed

+158
-1
lines changed

README-JunitTest.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SpringBoot使用Junit
2+
3+
## 引入依赖
4+
```aidl
5+
<dependency>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-test</artifactId>
8+
<scope>test</scope>
9+
</dependency>
10+
```
11+
## 测试
12+
+ 1.我们需要测试的类
13+
```aidl
14+
@Service
15+
public class HelloWorldService {
16+
public void sayHello() {
17+
System.out.println("Hello world");
18+
}
19+
}
20+
21+
```
22+
+ 2.测试
23+
```aidl
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@SpringBootTest(classes = App.class)
26+
public class HelloWorldServiceTest {
27+
28+
@Autowired
29+
private HelloWorldService helloWorldService;
30+
31+
@Test
32+
public void sayHello() throws Exception {
33+
helloWorldService.sayHello();
34+
}
35+
36+
}
37+
```
38+
## 注解说明
39+
+ 1.@RunWith(SpringJUnit4ClassRunner.class)用来引入SpringJunit支持.
40+
+ 2.@SpringBootTest(classes = App.class)用来加载我们的启动类,也就是启动Spring容器,这是1.4之后的
41+
注解,我们也可以用SpringApplicationConfiguration这个注解.
42+
+ 3.@WebAppConfiguration,如果是web项目,我们再测试时需要加载ServletContext的话,就需要加上这个注解.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#SpringBoot
1+
# SpringBoot

SpringBoot-JuintTest/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>SpringBoot-Study</artifactId>
7+
<groupId>com.lc</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>com.lc</groupId>
13+
<artifactId>SpringBoot-JuintTest</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<!-- Import dependency management from Spring Boot -->
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-dependencies</artifactId>
22+
<version>1.4.0.RELEASE</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
<executions>
48+
<execution>
49+
<goals>
50+
<goal>repackage</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author lsj <[email protected]>
8+
* @date 17-4-3
9+
*/
10+
@SpringBootApplication
11+
public class App {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(App.class, args);
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.lc.springBoot.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* @author lsj <[email protected]>
7+
* @date 17-4-3
8+
*/
9+
@Service
10+
public class HelloWorldService {
11+
public void sayHello() {
12+
System.out.println("Hello world");
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lc.springBoot.service;
2+
3+
import com.App;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
/**
11+
* @author lsj <[email protected]>
12+
* @date 17-4-3
13+
*/
14+
@RunWith(SpringJUnit4ClassRunner.class)
15+
@SpringBootTest(classes = App.class)
16+
public class HelloWorldServiceTest {
17+
18+
@Autowired
19+
private HelloWorldService helloWorldService;
20+
21+
@Test
22+
public void sayHello() throws Exception {
23+
helloWorldService.sayHello();
24+
}
25+
26+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module>SpringBoot-HelloWorld-2</module>
1414
<module>SpringBoot-GlobalExceptionHandler</module>
1515
<module>SpringBoot-FastJson</module>
16+
<module>SpringBoot-JuintTest</module>
1617
</modules>
1718

1819
</project>

0 commit comments

Comments
 (0)