本章介绍使用 Junit4 测试 Spring,关于 Junit4 的基本使用 参考:【Junit】Junit快速入门
一、项目框架
1、搭建一个Maven项目,引入 spring依赖、spring-test依赖 和 junit依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test.junit</groupId> 8 <artifactId>test-junit-spring</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <!-- spring --> 13 <spring.version>5.2.4.RELEASE</spring.version> 14 <!-- Junit4 --> 15 <junit.version>4.12</junit.version> 16 <maven.compiler.source>8</maven.compiler.source> 17 <maven.compiler.target>8</maven.compiler.target> 18 </properties> 19 20 <dependencies> 21 <!-- Spring --> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-context</artifactId> 25 <version>${spring.version}</version> 26 </dependency> 27 28 <!-- Spring Test 模块 --> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-test</artifactId> 32 <version>${spring.version}</version> 33 <scope>test</scope> 34 </dependency> 35 36 <!-- junit依赖 --> 37 <dependency> 38 <groupId>junit</groupId> 39 <artifactId>junit</artifactId> 40 <version>${junit.version}</version> 41 </dependency> 42 </dependencies> 43 44 </project>
2、准备一个Spring xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-4..xsd 9 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 11 12 <bean id="date" class="java.util.Date"></bean> 13 </beans>
二、使用Junit4 测试 Spring
方法1:利用 @BeforeClass + xml文件 启动Spring容器
1 package com.test.junite.spring; 2 3 import org.junit.BeforeClass; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import java.util.Date; 9 10 /** 11 * 利用 @BeforeClass 注解启动Spring容器,开始测试 12 * @BeforeClass:它会在所有方法运行前被执行,static修饰 13 */ 14 public class SpringTest { 15 16 private static ApplicationContext context = null; 17 18 @BeforeClass 19 public static void setUpBeforeClass() throws Exception { 20 context = new ClassPathXmlApplicationContext("applicationContext.xml"); 21 } 22 23 24 @Test 25 public void test() { 26 Date date = (Date) context.getBean("date"); 27 System.out.println(date); 28 } 29 }
方法2:利用 @BeforeClass + 配置类 启动Spring容器
1 package com.test.junite.spring; 2 3 import org.junit.BeforeClass; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 import java.util.Date; 11 12 /** 13 * 利用 @BeforeClass 注解启动Spring容器,开始测试 14 * @BeforeClass:它会在所有方法运行前被执行,static修饰 15 */ 16 public class SpringTest2 { 17 18 private static ApplicationContext context = null; 19 20 @BeforeClass 21 public static void setUpBeforeClass() throws Exception { 22 context = new AnnotationConfigApplicationContext(MyConfig.class); 23 } 24 25 26 @Test 27 public void test() { 28 Date date = (Date) context.getBean("date"); 29 System.out.println(date); 30 } 31 32 @Configuration 33 static class MyConfig { 34 @Bean 35 public Date date(){ 36 return new Date(); 37 } 38 } 39 }
方法3:利用 @RunWith运行器 + xml文件 启动Spring容器
1 /** 2 * 利用 @RunWith 运行器,来运行一个Spring环境,@ContextConfiguration 导入相关配置 3 * 4 * @RunWith作用 5 * @RunWith 就是一个运行器 6 * @RunWith(JUnit4.class) 就是指用JUnit4来运行 7 * @RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境 8 * @RunWith(Suite.class) 的话就是一套测试集合, 9 */ 10 // junit整合spring的测试 11 // SpringRunner 继承了SpringJUnit4ClassRunner, 12 // 没有扩展任何功能;使用SpringRunner,名字简短而已 13 @RunWith(SpringRunner.class) 14 // @RunWith(SpringJUnit4ClassRunner.class) 15 // 加载核心xml配置文件,自动构建spring容器 16 @ContextConfiguration(locations="classpath:applicationContext.xml") 17 public class SpringTest3 { 18 19 @Autowired 20 private ApplicationContext contex; 21 22 23 @Test 24 public void test() { 25 Date date = (Date) contex.getBean("date"); 26 System.out.println(date); 27 } 28 }
方法4:利用 @RunWith运行器 + 配置类 启动Spring容器
1 package com.test.junite.spring; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.Import; 10 import org.springframework.test.context.junit4.SpringRunner; 11 12 import java.util.Date; 13 14 // junit整合spring的测试 15 @RunWith(SpringRunner.class) 16 // 导入配置类,自动构建spring容器 17 @Import(SpringTest4.MyConfig.class) 18 public class SpringTest4 { 19 20 @Autowired 21 private ApplicationContext contex; 22 23 @Test 24 public void test() { 25 Date date = (Date) contex.getBean("date"); 26 System.out.println(date); 27 } 28 29 @Configuration 30 static class MyConfig{ 31 @Bean 32 public Date date() { 33 return new Date(); 34 } 35 } 36 }