Python 官方文档:入门教程 => 点击学习
目录spring-Test对Spring框架进行单元测试加载依赖编写SpringTestBase基础类,加载所需xml文件编写单元测试类 示例Spring-Test测试数据1、新建一
配置过程:
引入Maven依赖:
<!--spring单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework}</version>
<scope>test</scope>
</dependency>
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SprinGContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = { "classpath:Application-Redis.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTestBase extends AbstractJUnit4SpringContextTests {
}
将所需加载的xml文件指定为locations的value。
直接继承SpringTestBase 就可以对Spring框架的内容进行单元测试。
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class TestRedisCacheDaoImpl extends SpringTestBase {
@Autowired
public RedisCacheDaoImpl redisCacheDaoImpl;
@Test
public void testPing() {
boolean reslut = redisCacheDaoImpl.ping();
Assert.assertEquals(true, reslut);
}
}
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-WEBmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
spring-test是Spring当中的测试包,而junit是单元测试包,结合起来使用。
添加一个bean,便于测试。
HelloService.java
@Service
public class HelloService {
public String hello(String name) {
return "hello " + name;
}
}
添加配置文件扫描包:
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--将除了 Controller 之外的所有 Bean 注册到 Spring 容器中-->
<context:component-scan base-package="org.youyuan" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.youyuan.controller"/>
</context:component-scan>
</beans>
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class Test {
@Autowired
HelloService helloService;
@org.junit.Test
public void test(){
System.out.println(helloService.hello("youyuan"));
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 如何使用Spring-Test对Spring框架进行单元测试
本文链接: https://lsjlt.com/news/134713.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0