Spring提供了一个运行器,可以读取配置文件(或注解)来创建容器。
<dependencies>
<!--spring context依赖-->
<!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.1</version>
</dependency>
<!--spring对junit的支持相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.1.1</version>
</dependency>
<!--junit5测试-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
</dependency>
<!--log4j2的依赖-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.19.0</version>
</dependency>
</dependencies>
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.giser.spring6" />
</beans>
package com.giser.spring6.junit5;
import org.springframework.stereotype.Component;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:37:52
*/
@Component
public class User {
public User() {
System.out.println("run user");
}
}
package com.giser.spring6.junit5;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:41:24
*/
// 方式二
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:spring-bean.xml")
// 方式一 推荐
@SpringJUnitConfig(locations = "classpath:spring-bean.xml")
public class SpringJunit5Test {
@Autowired
private User user;
@Test
public void testUser(){
System.out.println(user);
}
}
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
package com.giser.spring6.junit4;
import org.springframework.stereotype.Component;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:37:52
*/
@Component
public class Junit4User {
public Junit4User() {
System.out.println("run junit4User");
}
}
package com.giser.spring6.junit4;
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;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:41:24
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-bean.xml")
public class SpringJunit4Test {
@Autowired
private Junit4User junit4User;
@Test
public void testUser(){
System.out.println(junit4User);
}
}