Junit和Spring
@ContextConfiguration 用来指定加载的Spring配置文件的位置,会加载默认配置文件
例如下例会加载:classpath:/com/example/MyTest-context.xml文件
- package com.example;
- @ContextConfiguration
public class MyTest {
// class body...
}
@ContextConfiguration 注解有以下两个常用的属性:
- locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。如下所示:
@ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”}) - inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。如下面的例子:
- @ContextConfiguration(locations={"base-context.xml"})
public class BaseTest {
// ...
}
@ContextConfiguration(locations={"extended-context.xml"})
public class ExtendedTest extends BaseTest {
// ...
}
如果 inheritLocations 设置为 false,则 ExtendedTest 仅会使用 extended-context.xml 配置文件,否则将使用 base-context.xml 和 extended-context.xml 这两个配置文件。
在使用所有注释前必须使用@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
Spring框架在org.springframework.test.annotation
包中提供了常用的Spring特定的注解集,如果你在Java5或以上版本开发,可以在测试中使用它。
@IfProfileValue
提示一下,注解测试只针对特定的测试环境。 如果配置的ProfileValueSource类返回对应的提供者的名称值, 这个测试就可以启动。这个注解可以应用到一个类或者单独的方法。
@IfProfileValue(name=”java.vendor”, value=”Sun Microsystems Inc.”)
public void testProcessWhichRunsOnlyOnSunJvm() {
// some logic that should run only on Java VMs from Sun Microsystems
}
同时@IfProfileValue可配置一个值列表 (使用OR 语义) 来在JUnit环境中获得TestNG的测试组支持。 看下面的例子:
@IfProfileValue(name=”test-groups”, values={”unit-tests”, “integration-tests”})
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
@ProfileValueSourceConfiguration
类级别注解用来指定当通过@IfProfileValue注解获取已配置的profile值时使用何种ProfileValueSource。 如果@ProfileValueSourceConfiguration没有在测试中声明,将默认使用 SystemProfileValueSource。
@ProfileValueSourceConfiguration(CustomProfileValueSource.class)
public class CustomProfileValueSourceTests {
// class body…
}
@DirtiesContext
在测试方法上出现这个注解时,表明底层Spring容器在该方法的执行中被“污染”,从而必须在方法执行结束后重新创建(无论该测试是否通过)。
@DirtiesContext
public void testProcessWhichDirtiesAppCtx() {
// some logic that results in the Spring container being dirtied
}
@ExpectedException
表明被注解方法预期在执行中抛出一个异常。预期异常的类型在注解中给定。如果该异常的实例在测试方法执行中被抛出, 则测试通过。同样的如果该异常实例没有在测试方法执行时抛出,则测试失败。
@ExpectedException(SomeBusinessException.class)
public void testProcessRainyDayScenario() {
// some logic that should result in an Exception being thrown
}
@Timed
表明被注解的测试方法必须在规定的时间区间内执行完成(以毫秒记)。如果测试执行时间超过了规定的时间区间,测试就失败了。
注意该时间区间包括测试方法本身的执行,任何重复测试(参见 @Repeat),还有任何测试fixture的set up或tear down时间。
Spring的@Timed
注解与JUnit 4的@Test(timeout=...)
支持具有不同的语义。 特别地,鉴于JUnit 4处理测试执行超时(如通过在一个单独的线程
中执行测试方法)的方式, 我们不可能在一个事务上下文中的测试方法上使用JUnit的@Test(timeout=...)
配置。因此, 如果你想将一个测试方法配置成计时且具事务性的, 你就必须联合使用Spring的@Timed
及@Transactional
注解。 还值得注意的是@Test(timeout=...)
只管测试方法本身执行的次数,如果超出的话立刻就会失败; 然而,@Timed
关注的是测试执行的总时间(包括建立和销毁操作以及重复),并且不会令测试失败。
@Timed(millis=1000)
public void testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to execute
}
@Repeat
表明被注解的测试方法必须重复执行。执行的次数在注解中声明。
注意重复执行范围包括包括测试方法本身的执行,以及任何测试fixture的set up或tear down。
@Repeat(10)
public void testProcessRepeatedly() {
// …
}
@Rollback
表明被注解方法的事务在完成后是否需要被回滚。 如果true,事务将被回滚,否则事务将被提交。 使用@Rollback接口来在类级别覆写配置的默认回滚标志。
@Rollback(false)
public void testProcessWithoutRollback() {
// …
}
@NotTransactional
出现该注解表明测试方法必须不在事务中执行。
@NotTransactional
public void testProcessWithoutTransaction() {
// …
}
Spring TestContext Framework还支持下面这些非特定于测试的注解,并且保持其语义不变。
@Autowired
@Qualifier
@Resource
(javax.annotation)如果JSR-250可用@PersistenceContext
(javax.persistence)如果JPA可用@PersistenceUnit
(javax.persistence)如果JPA可用@Required
@Transactional
@TestExecutionListeners
定义类级别的元数据,TestExecutionListener
s会使用TestContextManager
进行注册。 通常,@TestExecutionListeners
与@ContextConfiguration
会搭配使用。
- @ContextConfiguration
@TestExecutionListeners({CustomTestExecutionListener.class, AnotherTestExecutionListener.class})
public class CustomTestExecutionListenerTests {
// class body...
}
@TransactionConfiguration
为配置事务性测试定义了类级别的元数据。特别地,如果需要的PlatformTransactionManager不是“transactionManager”的话, 那么可以显式配置驱动事务的PlatformTransactionManager
的bean名字。此外, 可以将defaultRollback
标志改为false
。通常, @TransactionConfiguration
与@ContextConfiguration
搭配使用。
- @ContextConfiguration
@TransactionConfiguration(transactionManager="txMgr", defaultRollback=false)
public class CustomConfiguredTransactionalTests {
// class body...
}
@BeforeTransaction
表明被注解的public void
方法应该在测试方法的事务开始之前执行, 该事务是通过@Transactional
注解来配置的。
- @BeforeTransaction
public void beforeTransaction() {
// logic to be executed before a transaction is started
}
@AfterTransaction
表明被注解的public void
方法应该在测试方法的事务结束之后执行, 该事务是通过@Transactional
注解来配置的。
- @AfterTransaction
public void afterTransaction() {
// logic to be executed after a transaction has ended
}
Junit和Spring的更多相关文章
- Junit/idea Junit支持/Spring test之间的孽世纠葛
最近应老板要求,研究研究Spring测试相关的东西,力求搞一个方便使用的测试工具,对于一个Spring不熟Junit不懂的人这是一个很大的坑,扫了一边spring test文档没感觉有什么收获,spr ...
- 使用junit进行Spring测试
这几天在做SpringMVC的项目,现在总结一下在测试的时候碰到的一些问题. 以前做项目,是在较新的MyEclipse(2013)上面进行Maven开发,pom.xml 文件是直接复制的,做测试的时候 ...
- Maven下使用Junit对Spring进行单元测试
主要步骤 1. 在工程的pom文件中增加spring-test的依赖: <dependency> <groupId>org.springframework</groupI ...
- junit单元测试+junit与Spring结合
配置:右键要加入单元测试的工程,选择properties,然后选择java build path,选择add library,选择junit即可. 编写:右键要测试的class,new一个junit ...
- Junit测试 - Spring的配置
第一种: @ContextConfiguration(locations = {"classpath*:/spring-core.xml"}) public class UserM ...
- 使用Junit对Spring进行单元测试实战小结
Demo代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/ ...
- Junit结合Spring对Dao层进行单元测试
关于单元测试,上一次就简单的概念和Mock基础做了,参考:http://60.174.249.204:8888/in/modules/article/view.article.php/74 实际开发过 ...
- Junit测试Spring应用Dubbo测试框架之-Excel 工具类
package com.tree.autotest.demo; import com.alibaba.fastjson.JSON;import org.apache.poi.hssf.usermode ...
- Junit 测试 Spring
在测试类上加上@RunWith,和@ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration ...
随机推荐
- mysql慢查询配置
1.慢查询有什么用? 能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? 首先我们先查看MYSQL服 ...
- 详解linux中的ps命令
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- git diff 打补丁
[root@workstation2017 demo]# git diff old new >cc.diff[root@workstation2017 demo]# cat cc.diffdif ...
- 使用C#中的ref关键字,用2个简单例子来说明
在C#中,如果在方法参数前面加上ref关键字,说明参数传递的是引用,而不是值.如何理解呢? 参数是简单类型的例子 static void Main(string[] args) { string te ...
- extjs 动态设定 DateField 最大值 最小值
yxrqDate.minValue = new Date();yxrqDate.maxValue = new Date(9000,1,1);yxrqDate.validate(); //var pic ...
- 转: Go -- 单例
最近在学习Golang,想着可以就以前的知识做一些串通,加上了解到go语言也是面向对象编程语言之后.在最近的开发过程中,我碰到一个问题,要用go语言实现单例模式.本着“天下知识,同根同源”(我瞎掰的~ ...
- UIScrollView视差效果动画
UIScrollView视差效果动画 效果 源码 https://github.com/YouXianMing/Animations // // ScrollImageViewController.m ...
- JavaScript 触发click事件 兼容FireFox,IE 和 Chrome
解决了火狐下无法触发click事件的问题 <script language="javascript"> function test2(name) { if(docume ...
- JAVA复制文件最快的算法
/** * 复制文件 * * @param srcFile * 源文件File * @param destDir * 目标目录File * @param newFileName * 新文件名 * @r ...
- Spring Boot中application.yml与bootstrap.yml的区别(转)
说明:其实yml和properties文件是一样的原理,主要是说明application和bootstrap的加载顺序.且一个项目上要么yml或者properties,二选一的存在. Bootstra ...