【Spring Boot 快速入门】四、Spring Boo

这是我参与8月更文挑战的第10天,活动详情查看:8月更文挑战

收录专栏

Spring Boot 快速入门

Java全栈架构师

前言

  我们在开发的时候经常需要写一写测试代码,需要验证已完成的功能是否按照预先设计的业务逻辑运行,这时候就会使用到单元测试,当然使用junit写一些适当的测试能够快速检测业务逻辑的正确性,及时调整优化我们的代码。本文将开始介绍Spring Boot集成JUnit,下面开始介绍JUnit。

什么JUnit

  Junit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck
的sUnit的xUnit家族中最为成功的一个。JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集
成了JUnit作为单元测试的工具。JUnit作为目前Java领域内最为流行的单元测试框架已经走过了数十年。

JUnit作用

  Junit能让我们快速的完成单元测试,而且编写测试和编写代
码都是增量式的,写一点测一点,在编写以后的代码中如果发现问题可以较快的追踪到问题的原因,减小
回归错误的纠错难度。

JUnit特性

  JUnit是一个开放源代码的Java测试框架,用于编写和运行可重复的测试。他是用于单元测试框架体系xUnit的一个实例(用于java语言)。它包括以下特性:
1、用于测试期望结果的断言(Assertion)
2、用于共享共同测试数据的测试工具
3、用于方便的组织和运行测试的测试套件
4、图形和文本的测试运行器

JUnit优点

  • 安装简单
  • 极限编程
  • 重构

快速开始

引包

  本次测试使用的是Maven构建的Java项目,所以首先应该在pom.xml中引入相应的依赖包。具体使用需要与所使用的框架版本结合起来。找到合适的版本。本文使用的依赖如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
js复制代码<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- junjit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

常用注解

常用注解 描述
@Test 将一个方法标记为测试方法
@Before 每一个测试方法调用前必执行的方法
@After 每一个测试方法调用后必执行的方法
@BeforeClass 所有测试方法调用前执行一次,在测试类没有实例化之前就已被加载,需用static修饰
@AfterClass 所有测试方法调用后执行一次,在测试类没有实例化之前就已被加载,需用static修饰
@lgnore 暂不执行该方法
@Timeout 表示测试方法运行如果超过了指定时间将会返回错误
@ExtendWith 为测试类或测试方法提供扩展类引用
@Disabled 表示测试类或测试方法不执行

执行顺序为:@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

Spring Boot注解

  • @SpringBootTest(classes = DemoJunitApplication.class):引入的一个用于测试的注解
  • @RunWith(SpringRunner.class),是一个运行器,指定运行的类,让测试在Spring容器环境下执行。
  • @Rollback:数据回滚,这样避免测试数据污染数据库
  • @AutoConfigureMockMvc:自动配值

项目结构

图片.png

源码

pom.xml

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
js复制代码<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>junit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>junit</name>
<description>Demo project for Spring Boot and MyBatis and Swagger</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- commons start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- commons end -->

<!-- mybatis start-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.11</version>
</dependency>
<!-- mybatis end-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- junjit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
js复制代码package com.example.demo;

import com.example.demo.controller.TestController;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = DemoJunitApplication.class)
@RunWith(SpringRunner.class)
@Rollback
@AutoConfigureMockMvc
class DemoApplicationTests {

@Autowired
private TestController testController;

@Autowired
private UserService userService;

/**
* @ClassName 测试接口
* @Description: 获取所有用户
* @Author JavaZhan @公众号:Java全栈架构师
* @Date 2020/6/13
* @Version V1.0
**/
@Test
void getTest() {
System.out.println(testController.getTest());

}


/**
* @ClassName getAllUser
* @Description: 获取所有用户
* @Author JavaZhan @公众号:Java全栈架构师
* @Date 2020/6/13
* @Version V1.0
**/
@Test
void getAllUser() {
userService.getAllUser().forEach(user -> System.out.println(user));

}

}

运行结果

图片.png

结语

  本次基于Spring Boot集成JUnit的项目就完成了,相信你对JUnit的单元测试有了一个简单的了解,希望本文可以帮助到你。感谢阅读。

  作者介绍:【小阿杰】一个爱鼓捣的程序猿,JAVA开发者和爱好者。公众号【Java全栈架构师】维护者,欢迎关注阅读交流。

  好了,感谢您的阅读,希望您喜欢,如对您有帮助,欢迎点赞收藏。如有不足之处,欢迎评论指正。下次见。

推荐阅读:

我的第一个Spring Boot项目启动啦!

周末建立了Spring Boot专栏,欢迎学习交流

Spring Boot集成MyBatis,可以连接数据库啦!

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%