1.Spring支持JUnit4136
准备工作:
pom.xml<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.powernode</groupId> <artifactId>course32</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <!--仓库--> <repositories> <!--仓库里程碑版spring--> <repository> <id>repository.spring.milestone</id> <name>Spring Milestone Repository</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <dependencies> <!--spring context依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.0-M2</version> </dependency> <!--spring依赖junit支持 136--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <!--这个版本的spring6既支持JUnit4,也支持JUnit5.--> <version>6.0.0-M2</version> </dependency> <!--依赖junit4--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties></project>
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.powernode.spring6.bean"/></beans>
Userpackage com.powernode.spring6.bean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * 普通的bean **/@Componentpublic class User { @Value(张三) private String name; @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User() { } public User(String name) { this.name = name; }}
测试SpringJunit4Testttest这些注释主要是Spring提供的便利:
@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
在单元测试类中使用这两个注释后,可以使用单元测试类中的属性@Autowired。比较方便。
package com.powernode.spring6.test;import com.powernode.spring6.bean.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJunit4ClassRunner;/** * 支持JUnit4的Spring 136 **/@RunWith(SpringJunit4ClassRunner.class) ///这是junit中的注释@ContextConfiguration("classpath:spring.xml()//这是spring框架中的注解public class SpringJunit4Test { @Autowired private User user; ///注入User类 @Test public void testUser(){ /*ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); User user = applicationContext.getBean("user", User.class);*/ System.out.println(user.getName()); } @Test public void testuser2(){ /*ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); User user = applicationContext.getBean("user", User.class);*/ System.out.println(user.getName()); } @Test public void testuser3(){ /* ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); User user = applicationContext.getBean("user", User.class);*/ System.out.println(user.getName()); }}
2.支持JUnit5的Spring137Spring依赖Junit5,依赖Junit支持:spring-test,如下:
pom.xml<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.powernode</groupId> <artifactId>spring6-015-junit</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <!--仓库--> <repositories> <!--仓库里程碑版spring--> <repository> <id>repository.spring.milestone</id> <name>Spring Milestone Repository</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <dependencies> <!--spring context依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.0-M2</version> </dependency> <!--支持Junit的spring相关依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>6.0.0-M2</version> </dependency> <!--依赖junit5--> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.0</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties></project>
测试SpringJunit5testtestpackage com.powernode.spring6.test;import com.powernode.spring6.bean.User;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.extension.ExtendWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit.jupiter.SpringExtension;/** * 支持JUnit5的Spring 137 **/@ExtendWith(SpringExtension.class)@ContextConfiguration("classpath:spring.xml")public class SpringJunit5Test { @Autowired private User user; @Test public void testUser(){ System.out.println(user.getName()); }}