您的位置:首页 > 其它

Junit框架使用(3)--按照顺序执行测试用例

2014-10-12 23:04 134 查看
有时候我们需要按照顺序执行我们的单元测试方法,如在测试Dao层的时候要按照测试插入方法、查询方法、更新方法、删除方法的顺序测试。如果不按照这个顺序测试可能会出现问题,而Junit测试时默认的顺序是随机的。如果插入方法在后面执行可能前面的方法都不能通过测试,因为数据还没有插入。

下面是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:对方法名升序排序,然后按照排序后的顺序执行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: