SpringBoot基本操作(六)——SpringBoot使用Junit4单元测试(有demo)

本文介绍了如何在SpringBoot项目中使用Junit4进行单元测试,包括IDEA下载安装Junit插件,引入测试依赖,创建被测试类UserController,生成并改造测试类UserControllerTest,详细讲解测试方法的编写和执行,提供了一个实际的测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot2.0笔记

(一)SpringBoot基本操作——环境搭建及项目创建(有demo)

(二)SpringBoot基本操作——使用IDEA打war包发布及测试

(三)SpringBoot基本操作——SpringBoot整合SpringDataJpa(有demo)

(四)SpringBoot基本操作——SpringBoot使用RedisTemplate整合Redis(有demo)

(五)SpringBoot基本操作——SpringBoot使用Jedis整合Redis(有demo)

(六)SpringBoot基本操作——SpringBoot使用Junit4单元测试(有demo)

(七)SpringBoot基本操作——SpringBoot整合Shiro权限管理(完整demo+界面)

 

本篇介绍Springboot单元测试的一些基本操作,有人说一个合格的程序员必须熟练使用单元测试,接下来我们一起在Springboot项目中整合Junit4单元测试。

本文使用idea工具构建Springboot2.0+SpringMvc+Thymeleaf+SpringDataJPA+MySql项目

GitHub地址:https://github.com/jwam/springbootJunit4.git

一、IDEA下载并安装Junit插件

点击file-settings,如图按照顺序操作即可,我这里已经安装过了所以最后一步不会显示install按钮。

 二、引入关键依赖,完整pom文末给出

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<!--<scope>test</scope>-->
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>5.0.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-test</artifactId>
			<version>2.0.0.RELEASE</version>
		</dependency>

三、新建UserController.java作为被测试类

package com.springboot.demo.controller;

import com.springboot.demo.base.controller.BaseController;
import com.springboot.demo.base.utils.StateParameter;
import com.springboot.demo.entity.User;
import com.springboot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframewo
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值