之前测试代码:
@Test
public void testAddOld() {
ApplicationContext appContext = new ClassPathXmlApplicationContext("application.xml");
appContext.getBean("testTbService");
TestTb testTb = new TestTb();
testTb.setName("小虫");
testTb.setBirthday(new Date());
testTbService.addTestTb(testTb);
}
之前测试service时都是先加载spring的主配置文件,然后getBean从IOC容器中得到service的实例再进行相应的测试。比较麻烦。
package cn.smallbug;
import java.util.Date;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.smallbug.core.bean.TestTb;
import cn.smallbug.core.service.TestTbService;
/**
*
* @timestamp Jan 31, 2016 11:27:24 PM
* @author smallbug
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class TestTestTb {
@Resource
private TestTbService testTbService;
@Test
public void testAdd() {
TestTb testTb = new TestTb();
testTb.setName("小虫");
testTb.setBirthday(new Date());
testTbService.addTestTb(testTb);
}
}
殊不知其实是可以在类上加两行注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
再@Resource注入,直接就可以测试service了。而且可以再写个抽象类,加上上面两句注解,谁要测试直接继承这个类就行了,非常方便。
分享到:
相关推荐
通过Spring集成单元测试,以后测试service层内容时,就不用getBean()来获得Service层对象了
java_junit集成_亲测可用_含有jar包以及源码,和spring 集成
junit4单元测试使用步骤 参数化单元测试示例 spring集成junit4示例
spring3 + mybatis3 + junit4 可运行项目示例 可进行单元测试,集成测试。。。
主要介绍了Springboot整合junit过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
如何将Spock模拟注入到Spring集成测试中该项目旨在用作示例指南,以说明如何将Spock与Spring(和Spring Boot)结合使用,并结合使用Spring配置和Spock模拟。 有时,您想使用整个Spring上下文进行更完整的集成测试。 ...
Spring集成JUnit4,进行单元测试,包括数据和服务器mock等等
Spring对Junit4支持,可以通过注解方便的测试Spring程序 方便集成各种优秀框架 Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-spring-1.2.xsd(ehcache-spring-annotations...
与Spring Boot,Elasticsearch,Testcontainers和JUnit5的集成测试 一个简单的示例,演示测试容器,Elasticsearch和JUnit 5如何一起玩。 二手技术 Spring Boot 2.4.3 Kotlin1.4.31 带有Elasticsearch 7.10.1 ...
1.30 Spring Boot Junit单元测试 1.31 SpringBoot启动时的Banner设置 1.32 Spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖项的版本 1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@...
8、Spring集成Junit测试 9、Spring集成web环境 10、Spring JDBCTemplate基本使用 11、SpringAOP简介和快速入门 12、Spring XML方式配置AOP 13、Spring注解方式配置AOP 14、Spring编程式事务控制介绍 15、Spring基于...
Thymeleaf+Spring 测试项目该项目的目的是研究 Spring 和 Thymeleaf 作为 Eionet 软件的新软件标准。 设置为E-PRTR的demo,供大家讨论改写使用。 该应用程序使用 Liquibase 创建和升级数据库,并使用 Thymeleaf 作为...
里面演示了spring ioc的基本使用,同时使用了junit进行单元测试,后期会加入spring集成junit。 工程使用Maven管理,下载工程后,请使用mvn clean, mvn compile指令对工程进行编译,通过后,使用mvn eclipse:eclipse...
15. spring-test-4.1.1.RELEASE.jar 支持Spring组建JUnit和TestNG的单元测试和集成测试。 16. spring-tx-4.1.1.RELEASE.jar 17. spring-web-4.1.1.RELEASE.jar 包含Web应用开发时,用到Spring框架时所需的核心类,...
@RunWith 指定测试类使用的某个运行器参数SpringJUnit4ClassRunner.class @Parameters 指定参数类的参数数据集合 @Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为 @FixMethodOrder 指定测试方法的执行...
Spring对Junit4支持 可以通过注解方便的测试Spring程序 5. 方便集成各种优秀框架 Spring不排斥各种优秀的开源框架 其内部提供了对各种优秀框架的支持 6. 降低JavaEE API的使用难度 对JavaEE开发中一些难用的API(JDBC...
集成了Spring 3.2.2 Hibernate 3.3.2 Struts 2.3.4 用JPA的注解实现orm 不用创建数据库表 只需本机上有oracle即可 用JSR注解进行Spring的...用Spring Test整合Junit4 进行测试并自动事务回滚 不对数据库造成持久化操作
Curso Udemy综合测试框架框架
spring-test.jar:对JUNIT等测试框架的简单封装 spring-tx.jar:为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。 spring-webmvc-portlet.jar:Spring MVC的增强 spring-websocket.jar: