spring junit单元测试
项目是有很多个功能块组成的,我们开发的时候,当我们开发出来一个功能,想要测试这个功能是否正确,不可能等到前端和后端全部写好了再进行测试,这样太浪费时间,有没有什么方法能直接测试后台的功能写的是否正确(比如:service这个模块的功能)?当然有,下面讲解一个Junit单元测试。
我们是在Maven中进行测试的:
在maven中新建一个java EE工程:
(1)打开maven,在左栏边,右键=>new=>Maven Project
(2)不要勾选create a simple project这一项,点击“next”
(3)选择后缀名为“webapp”这一栏,点击“next”
(4)填写“Group Id”和“Artifact Id”,点击“finish”
这样就可以创建一个javaEE工程了,但是一个完整的项目有四个目录,如下图:
但是它会缺少几个source folder,但是项目已经默认有,我们重新建缺少的那几个目录
使用Junit单元测试,需要两个依赖jar包 Junit和test
我们通过配置来导入这两个jar包及其所依赖的jar包,(通过maven中央仓库 https://mvnrepository.com/)
(1)在pom.xml文件里添加响应的jar依赖
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>oracle.com</groupId><artifactId>JunitTest</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>JunitTest Maven Webapp</name><url>http://maven.apache.org</url><dependencies><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-test --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.2.RELEASE</version><scope>test</scope></dependency></dependencies><build><finalName>JunitTest</finalName></build> </project>
下面我们通过一个小例子来说明,直接上代码:
在service层来测试,新建一个接口以及该接口的实现类
ISayHello.java的代码是:
package com.service;public interface ISayHello {public void sayHello(); }
SayHelloImpl.java的代码是:
package com.service.imple;import com.service.ISayHello;public class SayHelloImpl implements ISayHello{@Overridepublic void sayHello() {System.out.println("你好啊!!!程序员"); }}
spring.xml代码是:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="hello" class="com.service.imple.SayHelloImpl"></bean> </beans>
spring.xml是实例化的第三方,是spring中IoC容器帮我们实例化对象的。其中class属性是实现类的全路径,id是实例化对象的名称。
pom.xml代码是:(我们还要导入spring的核心jar包)
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>oracle.com</groupId><artifactId>JunitTest</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>JunitTest Maven Webapp</name><url>http://maven.apache.org</url><dependencies><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-test --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.2.RELEASE</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.1.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.1.RELEASE</version></dependency></dependencies><build><finalName>JunitTest</finalName></build> </project>
在src/test/java该目录项目新建一个JunitTset类进行测试:
JunitTset.java代码是:
package com.test;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.service.ISayHello;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring.xml") public class JunitTest {@Autowiredprivate ISayHello sayhello;@Testpublic void test() {sayhello.sayHello();} }
其中:
@RunWith(SpringJUnit4ClassRunner.class)
通过注解的形式,意思是使用JUnit4进行测试
@ContextConfiguration(locations="classpath:spring.xml")
通过注解的形式,意思是加载spring.xml配置文件
@Autowiredprivate ISayHello sayhello;
因为是引入了ISayHello抽象,这个抽象是一个接口,使用@Autowired注解形式来帮我们注入,@Autowired是根据抽象类型,自动去spring容器中,帮我们找到类型是ISayHello的实现类,然后把对象自动注入进来,我们就可以直接使用了该对象了,然后可以调用到该抽象里的方法了。
@Testpublic void test() {sayhello.sayHello();}
其中:@Test是说明是单元测试,必须有的。
运行结果是:
转载于:https://www.cnblogs.com/WQX-work24/p/9928110.html
spring junit单元测试相关推荐
- Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test
1 package cn.zmh.PingCe; 2 3 import org.junit.Test; 4 import org.springframework.jdbc.core.BeanPrope ...
- 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)
创建web项目,引入jar包 引入Spring配置文件 编写目标类,完成配置 编写测试类 Spring整合Junit单元测试 编写一个切面类 配置切面类,产生代理: <?xml version= ...
- 如何使用 Spring 整合 junit 单元测试
文章目录 1.测试类中的问题和解决思路 1.1.问题 1.2.解决思路分析 2.配置步骤 2.1.第一步:拷贝整合 junit 的必备 jar 包到 lib 目录 2.2.第二步:使用@RunWith ...
- 保姆级教程:Spring Boot 单元测试
作者 | 小名同学 来源 | https://eamonyin.blog.csdn.net/ 一. 单元测试的概念 概念: 单元测试(unit testing),是指对软件中的最小可测试单元进行检查和 ...
- Spring的单元测试
使用Spring的单元测试 * 1.导包:Spring单元测试包 * 2.@ContextConfiguration(locations = "") 使用它来指定Spring的配置 ...
- Spring Boot 单元测试二三事
本文翻译自:https://reflectoring.io/unit-... 原文作者:Tom Hombergs 译文原地址:https://weyunx.com/2019/02/04... 写好单元 ...
- SSM中进行Junit单元测试时无法注入service
场景 在SSM项目中进行Junit单元测试时调用外部的service时,在使用时打断点发现为空. 代码如下: public class AlipayTester {private PassOrderS ...
- spring boot单元测试
做一个稳健的开发,写一首漂亮的单元测试是少不了的 首先要分清几个概念:测试方法.测试类.测试集.测试运行器. 测试方法就是用 @Test 注解的一些函数. 测试类是包含一个或多个测试方法的一个 XxT ...
- Spring Boot 单元测试详解+实战教程
转载自 Spring Boot 单元测试详解+实战教程 Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring ...
最新文章
- TeaseR++:快速鲁棒的C++点云配准库介绍+英文版视频教程
- 用python定义一个员工类_Python:定义一个只有整数定义的类
- Java—JVM加载机制
- 《WCF技术内幕》翻译31:第2部分_第6章_通道:概述与正确认识通道。
- .NET Core多平台项目模板eShopOnContainers编译手记
- C++ 类的const成员函数
- android4.0.3去掉底部状态栏statusbar,全屏显示示例代码
- 神经网络相关的笔试题目集合(一)
- 从宏观的实现原理和设计本质入手,带你理解 AOP 框架的原理
- python-面试通关宝典
- VMware虚拟终端的下载及安装
- matlab传递函数状态方程转换,利用matlab对状态方程与传递函数转换
- string.h 详解
- 笔记本电脑外接显示屏的分辨率设置,外接显示屏分辨率总是低一点的解决方法
- 微信步数日历打卡小程序
- WiFi穿墙手册:解读天线、dbi、发射功率和无线信号的关系
- sql中查询最近一条记录
- 使用过的moment对象的用法(至2022/11/03)
- 不用傅里叶变换,提取某一频率的幅值和相位
- oracle awr监控报告,一个Oracle小白的AWR报告分析(一)
热门文章
- 查看静态库(.lib)和动态库(.dll)的导出函数的信息
- TNS-03505 oracle用户可以tnsping通,普通用户tnsping报错
- C++ STL 遍历 map 的时候如何删除其中的 element
- [Bash]kill指定的进程名
- vue+element实现树状表格的增删改查;使用el-table树形数据与懒加载实现树状表格增删改查
- [Redux/Mobx] Mobx和Redux有什么区别?
- 前端学习(3120):react-hello-react的setstate的使用
- 工作312:uni-时间戳处理
- [js] 异步请求重试策略有哪些呢?
- 前端学习(2670): vue3.0实战开始建立新项目功能清单