Spring框架整合单元测试
目录
一、配置文件方式
1.导入依赖
2.编写类和方法
3.配置文件applicationContext-test.xml
4.测试类
5.运行结果
二、全注解方式
1.编写类和方法
2.配置类
3.测试类
4.运行结果
每次进行单元测试的时候,都需要编写创建工厂,加载配置文件等相关操作,比较繁琐,现在Spring提供了整合Junit单元测试的技术,可以简化测试开发,所以必须首先要有Junit单元测试的环境,也就是要导入Junit单元测试的依赖,还有spring-test的依赖
这里用了两种方式去整合,一种是配置文件的方式,一种是全注解的方式
一、配置文件方式
1.导入依赖
需要导入spring的核心依赖、Junit单元测试的依赖和spring-test的依赖
<dependencies>
<!--spring的核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<!--日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
------------------------------------------------------
//主要是这个
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
-------------------------------------------------------
<!--连接池-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
------------------------------------------------------------
//也得有这个
<!--spring-test-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
----------------------------------------------------------
</dependencies>
2.编写类和方法
把该类交给spring去管理
public class User {
public void sayHello(){
System.out.println("Hello--------------");
}
}
3.配置文件applicationContext-test.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.xsd">
<!--整合单元测试-->
<bean id="user" class="com.test.pojo.User"></bean>
</beans>
4.测试类
import com.test.pojo.User;
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;
/*
* Spring整合Junit单元测试
* */
@RunWith(value = SpringJUnit4ClassRunner.class) //运行单元测试
@ContextConfiguration(value = "classpath:applicationContext-test.xml")//加载类路径下的配置文件
public class Demo {
//按类型自动注入
@Autowired
private User user;
@Test
public void run1(){
user.sayHello();
}
}
5.运行结果
二、全注解方式
依赖没有变,只是没有配置文件了,换成了配置类;
并且重新换了一个类Customer,没有用配置文件方式里的User类
1.编写类和方法
import org.springframework.stereotype.Component;
@Component
public class Customer {
public void save(){
System.out.println("保存客户");
}
}
2.配置类
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//声明当前是配置类
@ComponentScan(value = "com.test.pojo") //扫描包
public class SpringConfig {
}
3.测试类
import com.test.config.SpringConfig;
import com.test.pojo.Customer;
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;
@RunWith(value = SpringJUnit4ClassRunner.class) //运行单元测试
@ContextConfiguration(classes = SpringConfig.class)//加载配置类
public class Demo1 {
@Autowired //按类型注入
private Customer customer;
@Test
public void run1(){
customer.save();
}
}