Junit框架使用(3)--按照顺序执行测试用例
2014-10-12 23:04
134 查看
有时候我们需要按照顺序执行我们的单元测试方法,如在测试Dao层的时候要按照测试插入方法、查询方法、更新方法、删除方法的顺序测试。如果不按照这个顺序测试可能会出现问题,而Junit测试时默认的顺序是随机的。如果插入方法在后面执行可能前面的方法都不能通过测试,因为数据还没有插入。
下面是Dao层的方法
@Before用来初始化Bean,在每个方法执行时都会执行一次
@Ignore指定忽略此测试方法
@FixMethodOrder用来指定方法执行顺序,传入一个MethodSorters
DEFAULT会按照方法的顺序执行
NAME_ASCENDING:对方法名升序排序,然后按照排序后的顺序执行
下面是Dao层的方法
package com.tiamaes.bean.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Repository; import com.tiamaes.bean.GjdsBusSite; @Repository public class GjdsBusSiteDao { @Autowired private MongoTemplate mongoTemplate; public GjdsBusSite get(String id){ GjdsBusSite bean = this.mongoTemplate.findById(id, GjdsBusSite.class); return bean; } public void save(GjdsBusSite bean){ this.mongoTemplate.save(bean); } public void update(GjdsBusSite bean){ } public void remove(String id){ GjdsBusSite bean = this.get(id); this.mongoTemplate.remove(bean); } }下面是测试
package com.tiamaes.dao; import static org.junit.Assert.fail; import java.util.Date; import static org.junit.Assert.*; import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.tiamaes.bean.GjdsBusSite; import com.tiamaes.bean.dao.GjdsBusSiteDao; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") @FixMethodOrder(MethodSorters.DEFAULT) public class GjdsBusSiteDaoTest { private GjdsBusSite bean = new GjdsBusSite(); //private String id = UUID.randomUUID().toString(); private String id = "57706cd4-244e-4101-a317-ee34347c4168"; @Autowired private GjdsBusSiteDao gjdsBusSiteDao; @Before public void init(){ System.out.println(id); bean.setId(id); bean.setBusNo("125412"); bean.setLat(123.1111); bean.setLng(24.1111); bean.setSiteTime(new Date()); bean.setLineNo("B1"); } @Test public void testDao(){ assertNotNull(this.gjdsBusSiteDao); } @Test public void testSave() { this.gjdsBusSiteDao.save(bean); } @Test public void testGet() { GjdsBusSite entity = this.gjdsBusSiteDao.get(id); assertNotNull(entity); assertNotNull(id,entity.getId()); } @Test @Ignore public void testUpdate() { fail("Not yet implemented"); } @Test @Ignore public void testRemove() { //fail("Not yet implemented"); this.gjdsBusSiteDao.remove(id); GjdsBusSite e = this.gjdsBusSiteDao.get(id); assertNull(e); } }因为用到了Spring所以需要将Runner指定为SpringJUnit4ClassRunner并使用@ContextConfiguration指定配置文件。
@Before用来初始化Bean,在每个方法执行时都会执行一次
@Ignore指定忽略此测试方法
@FixMethodOrder用来指定方法执行顺序,传入一个MethodSorters
package org.junit.runners; import java.lang.reflect.Method; import java.util.Comparator; import org.junit.internal.MethodSorter; /** * Sort the methods into a specified execution order. * Defines common {@link MethodSorter} implementations. * * @since 4.11 */ public enum MethodSorters { /** * Sorts the test methods by the method name, in lexicographic order, * with {@link Method#toString()} used as a tiebreaker */ NAME_ASCENDING(MethodSorter.NAME_ASCENDING), /** * Leaves the test methods in the order returned by the JVM. * Note that the order from the JVM may vary from run to run */ JVM(null), /** * Sorts the test methods in a deterministic, but not predictable, order */ DEFAULT(MethodSorter.DEFAULT); private final Comparator<Method> fComparator; private MethodSorters(Comparator<Method> comparator) { this.fComparator = comparator; } public Comparator<Method> getComparator() { return fComparator; } }
DEFAULT会按照方法的顺序执行
NAME_ASCENDING:对方法名升序排序,然后按照排序后的顺序执行
相关文章推荐
- Junit--按照顺序执行测试用例
- selenium---unittest框架测试用例函数执行顺序 优先级
- selenium---unittest框架测试用例函数执行顺序 优先级
- Python之使用unittest框架和HTMLTestRunner.py文件实现多个测试用例执行与测试报告生成
- JUnit中按照顺序执行测试方式
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- Junit测试用例按顺序执行
- 对基于JUnit和Ant的测试用例执行过程使用Kieker(AspectJ)进行监控的方法
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- JUnit中按照顺序执行测试方式
- JUnit中按照顺序执行测试方式
- selenium---unittest框架测试用例函数执行顺序 优先级
- selenium的TestNG框架注解,测试集合,用例分组,依赖测试,特定顺序执行,跳过某个case执行,自定义日志,断言_06
- 【软件测试】使用JUnit框架实现Java单元测试
- Ant+JUnit+Cobertura执行测试用例时,应注意的问题(以Apache POI为例)
- 【JavaWeb各种异常汇总】测试框架的使用、导包有顺序
- Eclipse中使用Junit编写测试用例
- Junit指定测试执行顺序
- [Junit] 测试方法执行顺序
- JUnit中使用Hamcrest测试框架的assertThat断言【4.4新特性】