FixMethodOrder指定测试方法的执行顺序

  • 阿里云  >  教程中心   >  java教程  >  JUnit4:在测试用例中用FixMethodOrder指定测试方法的执行顺序  
  • JUnit4:在测试用例中用FixMethodOrder指定测试方法的执行顺序
  • 发布时间:2018-08-28 来源:网络 上传者:用户

    关键字:

    发表文章
  • 摘要:我们在写JUnit测试用例时,有时候需要按照定义顺序执行我们的单元测试方法,比如如在测试数据库相关的用例时候要按照测试插入、查询、删除的顺序测试。如果不按照这个顺序测试可能会出现问题,比如删除方法在前面执行,后面的方法就都不能通过测试,因为数据已经被清空了。而JUnit测试时默认的顺序是随机的。所以这时就需要有办法要求JUnit在执行测试方法时按照我们指定的顺序来执行。JUnit是通过@FixMethodOrder注解(annotation)来控制测试方法的执行顺序的。@Fi
  •        <p>我们在写JUnit测试用例时,有时候需要按照定义顺序执行我们的单元测试方法,比如如在测试数据库相关的用例时候要按照测试插入、查询、删除的顺序测试。如果不按照这个顺序测试可能会出现问题,比如删除方法在前面执行,后面的方法就都不能通过测试,因为数据已经被清空了。而JUnit测试时默认的顺序是随机的。所以这时就需要有办法要求JUnit在执行测试方法时按照我们指定的顺序来执行。</p> 
    

JUnit是通过@FixMethodOrder注解(annotation)来控制测试方法的执行顺序的。@FixMethodOrder注解的参数是org.junit.runners.MethodSorters对象,在枚举类org.junit.runners.MethodSorters中定义了如下三种顺序类型:

  • MethodSorters.JVM

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得到的方法顺序,也就是代码中定义的方法顺序)

  • MethodSorters.DEFAULT(默认的顺序)

Sorts the test methods in a deterministic, but not predictable, order() (以确定但不可预期的顺序执行)

  • MethodSorters.NAME_ASCENDING

Sorts the test methods by the method name, in lexicographic order, with Method.toString() used as a tiebreaker (按方法名字母顺序执行)

举例说明

以下的代码,定义了三个方法testAddAndGet,testSearch,testRemove,我设计的时候,是希望三个方法按定义的顺序来执行。

package test;

import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@FixMethodOrder(MethodSorters.JVM)//指定测试方法按定义的顺序执行
public class TestJNI {
private static final Logger logger = LoggerFactory.getLogger(TestJNI.class);
@Test
public void testAddAndGet(){
logger.info("test ‘addBean’ and ‘getBean’ ");
}

@Test
public final void testSearch() {
    logger.info("test search CODE from JNI memory...");
}
@Test
public final void testRemove() {
    logger.info("test remove CODE from JNI memory...");     
}   

}

如果@FixMethodOrder定义为MethodSorters.DEFAULT或去掉代码中的@FixMethodOrder注解,那么测试用便执行的顺序是

这并不是我要的结果,testRemove如果先执行了,testSearch肯定什么也找不到。

如果改成@FixMethodOrder(MethodSorters.JVM),则这个执行顺序才是我想要的顺序。

           以上是<a href="https://www.aliyun.com/jiaocheng/1426595.html">JUnit4:在测试用例中用FixMethodOrder指定测试方法的执行顺序</a>的内容,更多
                          的内容,请您使用右上方搜索功能获取相关信息。

    </span>

    <li class="pagation">
        <a href="https://www.aliyun.com/jiaocheng/1426594.html" class="next">上一篇</a>
        <a href="https://www.aliyun.com/jiaocheng/1426593.html" class="one">1</a>
        <a href="https://www.aliyun.com/jiaocheng/1426592.html">2</a>
        <a href="https://www.aliyun.com/jiaocheng/1426598.html">3</a>
        <a href="https://www.aliyun.com/jiaocheng/1426597.html">4</a>
        <a href="https://www.aliyun.com/jiaocheng/1426596.html" class="next">下一篇</a>

    </li>
    <li class="p-search">
        本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果你发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:zixun-group@service.aliyun.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。

    </li>
    <li class="relevant-article">
        相关文章
    </li>
    <li id="flex">
        <ul>
            
        </ul>
        <ul>
                        </ul>
    </li>
</ul>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值