Spring整合Junit详解
1,整合Junit4
maven引入spring-test 和 junit4
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>在test目录下创建测试类
@RunWith(SpringJUnit4ClassRunner.class) //启用Junit4
@ContextConfiguration("classpath:META-INF/context-junit.xml") //加载配置文件
public class SpringJunit4 {
@Autowired
private Machine machine;
@Test
public void test1() throws Exception {
System.out.println(machine.getObject());
}
}2,整合Junit5
1,maven引入spring-test 和 junit5
JUnit 5 =JUnit Platform+JUnit Jupiter+JUnit Vintage
<!-- junit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>2,在test目录下创建测试类
@ExtendWith(SpringExtension.class) //启用Junit5
@ContextConfiguration("classpath:META-INF/context-junit.xml") //加载配置文件
public class SpringJunit5 {
@Autowired
private Machine machine;
@Test
public void test1() throws Exception {
System.out.println(machine.getObject());
}
}说明:在spring5中,可以用复合注解 @SpringJUnitConfig(locations = {"classpath:META-INF/context-junit.xml"}) 代替@ExtendWith(SpringExtension.class) 和@ContextConfiguration("classpath:META-INF/context-junit.xml")
//@ExtendWith(SpringExtension.class) //启用Junit5
//@ContextConfiguration("classpath:META-INF/context-junit.xml") //加载配置文件
@SpringJUnitConfig(locations = {"classpath:META-INF/context-junit.xml"})
public class SpringJunit5 {
@Autowired
private Machine machine;
@Test
public void test1() throws Exception {
System.out.println(machine.getObject());
}
}到此这篇关于Spring整合Junit详解的文章就介绍到这了,更多相关Spring Junit内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
java mybatis如何操作postgresql array数组类型
这篇文章主要介绍了java mybatis如何操作postgresql array数组类型,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01
Mybatis-plus数据权限DataPermissionInterceptor实现
本文主要介绍了Mybatis-plus数据权限DataPermissionInterceptor实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-07-07


最新评论