Junit 4.11里增加了指定测试方法执行顺序的特性
测试类的执行顺序可通过对测试类添加注解 “@FixMethodOrder(value)” 来指定,其中value 为执行顺序
三种执行顺序可供选择:默认(MethodSorters.DEFAULT),按方法名(MethodSorters.NAME_ASCENDING)和JVM(MethodSorters.JVM)
当没有指定任何顺序时,按默认来执行
Sorters
1. MethodSorters.DEFAULT
默认顺序由方法名hashcode值来决定,如果hash值大小一致,则按名字的字典顺序确定
由于hashcode的生成和操作系统相关(以native修饰),所以对于不同操作系统,可能会出现不一样的执行顺序,在某一操作系统上,多次执行的顺序不变
2. MethodSorters.NAME_ASCENDING (推荐)
按方法名称的进行排序,由于是按字符的字典顺序,所以以这种方式指定执行顺序会始终保持一致;
不过这种方式需要对测试方法有一定的命名规则,如 测试方法均以testNNN开头(NNN表示测试方法序列号 001-999)
3. MethodSorters.JVM
按JVM返回的方法名的顺序执行,此种方式下测试方法的执行顺序是不可预测的,即每次运行的顺序可能都不一样(JDK7里尤其如此).
.
package com.kingmed.ms.customer.biz;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.FixMethodOrder;
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.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kingmed.ms.customer.CustomerConstant;
import com.kingmed.ms.customer.ctrl.KmctCompanyCustomerCtrl;
import com.kingmed.ms.customer.entity.CustomerDoctor;
import com.kingmed.ms.customer.entity.CustomerDoctorEdit;
import com.kingmed.ms.customer.entity.CustomerInfo;
import com.kingmed.ms.customer.entity.CustomerInfoEdit;
import com.kingmed.ms.customer.vo.KmctPersonalCheckVO;
import com.kingmed.ms.customer.vo.KmctPersonalVO;
import com.kingmed.ms.util.DateTimeUtils;
import com.kingmed.ms.util.StringUtil;
import com.kingmed.platform.modules.common.web.vo.datagrid.ResultVO;
/**
*〈一句话功能简述〉
* 功能详细描述
* @author wind
* @create 2015年3月16日 下午5:05:54
* @version 1.0.0
* @see [相关类/方法](可选)
* @since [KM/MS] (可选)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/applicationContext.xml"})
@ActiveProfiles("development")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class KmctPersonalCustomerBizTest {