使用jUnit spring来联合测试
来源:互联网 发布:隆兴北伐 知乎 编辑:程序博客网 时间:2024/06/06 12:39
最近有点小闲,顺便研究一下spring2.5的注释ioc和就jUnit4的组合来写测试用例,发现非常方便
spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"><bean id="testService" class="cn.lik.airTycoon.JunitTestServiceImpl"></bean></beans>
service:
package cn.lik.airTycoon;public class JunitTestServiceImpl {public JunitTestServiceImpl() {System.out.println("....init....");}public boolean isReal(boolean is) {return !is;}}
测试用例1, 用的注释注入,这个注释注入是可以被继承的,也就是说写个抽象 类,配置好配置文件就可以了,其他测试用例继承这个就好
package test.airTycoon;import junit.framework.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.lik.airTycoon.JunitTestServiceImpl;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:applicationContext.xml" })public class AirtyCoonTest {@Autowiredprivate JunitTestServiceImpl testService;@Testpublic void test() {Assert.assertTrue(testService.isReal(false));}public JunitTestServiceImpl getTestService() {return testService;}public void setTestService(JunitTestServiceImpl testService) {this.testService = testService;}}
用例2 这个就没有写spring 的配置文件以及运行类,直接继承过来
package test.airTycoon;import org.junit.Assert;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import cn.lik.airTycoon.JunitTestServiceImpl;public class AirtyCoonTest2 extends AirtyCoonTest {@Autowiredprivate JunitTestServiceImpl testService;@Testpublic void test() {Assert.assertTrue(testService.isReal(false));}}
联合测试: 需要测试的类追加在后面就可以。
package test.airTycoon;import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;@RunWith(Suite.class)@SuiteClasses({ // test ListAirtyCoonTest.class, //AirtyCoonTest2.class //})public class AllTests {}
相当的方便阿,而且eclipse原生就是支持jUnit了,加上spring还有对junit的支持类,可以做到测试用例结束以后自动回滚数据库数据,完全不用担心由于测试而形成的垃圾数据
- 使用jUnit spring来联合测试
- 使用junit进行Spring测试
- 学会使用JUnit来测试程序
- spring结合Junit测试使用小结
- 在spring中使用junit测试
- Junit使用教程--整合spring测试套件
- spring mvc 使用junit 测试 controller
- Spring boot之使用Junit测试
- 使用Ant+JUnit+Cobertura来实现代码覆盖自动测试
- JUnit 测试 Spring
- spring junit 测试
- Spring的Junit测试
- spring junit测试
- spring junit集成测试
- spring junit集成测试
- junit 测试 spring
- Spring与junit测试
- JUnit 测试spring项目
- Java注释(Annotation)详解
- 浏览器内核WebKit编年史
- 正则表达式30分钟入门教程
- sscanf函数用法
- JS [ 压缩 ]
- 使用jUnit spring来联合测试
- Android 切换输入法
- 如何查看Windows xp是32位还是64位
- 说服(营销)的十大步骤【网络】
- c语言程序设计现代算法第8章第9题
- 安装和使用花生壳(linux)
- asp.net窗口拖拽问题
- WPF中自定义只能输入数字的TextBox .
- 横竖屏切换