Skip to content

Commit 74afe2b

Browse files
committed
feat: 添加单元测试示例
1 parent 18ff447 commit 74afe2b

File tree

6 files changed

+67
-5
lines changed

6 files changed

+67
-5
lines changed

codes/javatech/javatech-mq/javatech-kafka-springboot/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<parent>
7-
<groupId>io.github.dunwu</groupId>
8-
<artifactId>dunwu-starter-parent</artifactId>
9-
<version>0.5.6</version>
7+
<groupId>io.github.dunwu.boot</groupId>
8+
<artifactId>dunwu-boot</artifactId>
9+
<version>1.0.8</version>
1010
</parent>
1111

1212
<groupId>io.github.dunwu.javatech</groupId>

codes/javatech/javatech-server/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@
7575
<groupId>org.springframework</groupId>
7676
<artifactId>spring-webmvc</artifactId>
7777
</dependency>
78+
<dependency>
79+
<groupId>org.springframework</groupId>
80+
<artifactId>spring-test</artifactId>
81+
</dependency>
82+
<dependency>
83+
<groupId>junit</groupId>
84+
<artifactId>junit</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.assertj</groupId>
89+
<artifactId>assertj-core</artifactId>
90+
<scope>test</scope>
91+
</dependency>
7892
<!-- spring end -->
7993
</dependencies>
8094

codes/javatech/javatech-server/src/main/java/io/github/dunwu/javatech/controller/HelloController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.github.dunwu.javatech.controller;
22

3+
import io.github.dunwu.javatech.service.HelloService;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
57
import org.springframework.stereotype.Controller;
68
import org.springframework.web.bind.annotation.RequestMapping;
79
import org.springframework.web.bind.annotation.RequestMethod;
@@ -21,6 +23,9 @@ public class HelloController {
2123

2224
private final Logger log = LoggerFactory.getLogger(this.getClass());
2325

26+
@Autowired
27+
private HelloService helloService;
28+
2429
/**
2530
* <p>
2631
* 在本例中,Spring将会将数据传给 hello.jsp
@@ -31,7 +36,7 @@ public class HelloController {
3136
public ModelAndView hello(@RequestParam("name") String name) {
3237
ModelAndView mav = new ModelAndView();
3338
mav.addObject("message", "你好," + name);
34-
mav.setViewName("hello");
39+
mav.setViewName(helloService.hello());
3540
return mav;
3641
}
3742

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.dunwu.javatech.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* @author 11123558
7+
* @date 2022-07-28
8+
*/
9+
@Service
10+
public class HelloService {
11+
12+
public String hello() {
13+
return "hello";
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.dunwu.javatech;
2+
3+
import io.github.dunwu.javatech.service.HelloService;
4+
import org.assertj.core.api.Assertions;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
11+
/**
12+
* 单元测试
13+
* @author 11123558
14+
* @date 2022-07-28
15+
*/
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@ContextConfiguration(locations = {"classpath:spring/spring-servlet.xml"})
18+
public class HelloControllerTests {
19+
20+
@Autowired
21+
private HelloService service;
22+
23+
@Test
24+
public void test() {
25+
String msg = service.hello();
26+
Assertions.assertThat("hello").isEqualTo(msg);
27+
}
28+
}

docs/02.JavaEE/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ permalink: /pages/ca58e7/
5959

6060
## 🚪 传送
6161

62-
🏠 [JAVA-TUTORIAL 首页](https://github.com/dunwu/java-tutorial) ◾ 🎯 [我的博客](https://github.com/dunwu/blog)
62+
💧 [钝悟的 IT 知识图谱](https://dunwu.github.io/waterdrop/) ◾ 🎯 [钝悟的博客](https://dunwu.github.io/blog/)

0 commit comments

Comments
 (0)