参考地址:
[url] http://www.cnblogs.com/rainisic/archive/2012/01/22/spring_test_framework.html[/url]
1加入所需要的包
org.springframework.test-3.0.7.RELEASE.jar
JUnit 4
以及项目所需要的包
spring的东西在这里下载
http://www.springsource.org/download
junit的东西在这里下载
https://github.com/KentBeck/junit/downloads
2测试类应该继承与 AbstractJUnit4SpringContextTests 或 AbstractTransactionalJUnit4SpringContextTests
看具体的例子,
首先写一个BaseTest类,然后再写一个测试类,用测试类来继承BaseTest类,这样写的目的是避免每次写测试类都要加例如下面的东西,特别是如果需要加载的文件比较多时,这样写更好一点
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
BaseTest
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class BaseTest extends AbstractJUnit4SpringContextTests {
public BaseTest() {
super();
}
}
下面是测试类
Service1Test
import javax.annotation.Resource;
import org.junit.Test;
import com.test.BaseTest;
public class Service1Test extends BaseTest{
@Resource
private Service1 service1;
@Test
public void testMethod1(){
service1.method1();
}
@Test
public void testMethod2(){
service1.method2();
}
}
这样这个测试类就可以直接用junit来运行进行测试了。
备注:junit最好用带hamcrest版本的不然可能会出现下面的错误
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
然后spring中用到的包也最好是统一的某一个版本,不要测试的是一个版本,另外的是其它的版本,不然容易出现错误
分享到:
相关推荐
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
struts2:struts2-convention-plugin-2.3.1.2.jar ---注解开发 struts2-spring-plugin-2.3.1.2.jar---用于整合spring ... 整合Junit测试: spring-test-3.2.4.RELEASE.jar 连接池: c3p0-0.9.5.2.jar
多数Java的开发环境都已经集成了JUnit作为单元测试的工具。 [1] JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为...
使用集成开发环境对象,类,方法,字段和构造函数原始类型和引用算术表达式,字符串运算软件工程过程概述使用JUnit框架进行单元测试条件语句和布尔表达式迭代基本控制台和文本文件I / O 一维和二维数组使用符号调试...
struts2.1.8 spring2.5.6 hibernate3.3.2框架整合、分页演示,由于上传资源过大,相关jar包截图在压缩包内,请自行查找加入,另外junit里的save方法请注意下,由于我设置的是插入了2万条记录,测试分页速度,所以...
群集监视器[]:实现了Linux群集监视代理,该代理记录每个主机的硬件规格并实时监视其内存使用情况。 该项目主要是使用与PostgreSQL数据库集成的bash脚本开发的。 使用Docker进行容器化,最终产品重量轻,用途广泛且...
该项目使用了诸如Docker,Kubernetes,用于日志记录和监视的Elasticsearch Stack,Java SE 15,MySQL和MongoDB数据库之类的尖端技术,所有组件都考虑了TDD的开发,涵盖了集成和性能测试等等。 该项目将作为阶段...
logo 设计不会,就这样先凑合了写道 Fuseblog预备...IVY,它是一个管理(记录、跟踪、解析和报告)项目依赖的工具,可与ApacheAnt紧密集成,很多的信息,请参照: http://ant.apache.org/ivy 1.代码可以从这里得到 svn co ...
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...