Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 名称升序@FixMethodOrder不适用于Junit测试_Java_Junit - Fatal编程技术网

Java 名称升序@FixMethodOrder不适用于Junit测试

Java 名称升序@FixMethodOrder不适用于Junit测试,java,junit,Java,Junit,在测试我自己的ArrayList实现时,我在测试之前设置了MyArrayList类的一个实例,为了检查我的逻辑在我使用@FixMethodOrder(MethodSorters.NAME_)的实现方法中是否正常工作。但是,一旦我运行了测试,执行的测试的顺序就不是字典式的 import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.a

在测试我自己的ArrayList实现时,我在测试之前设置了MyArrayList类的一个实例,为了检查我的逻辑在我使用@FixMethodOrder(MethodSorters.NAME_)的实现方法中是否正常工作。但是,一旦我运行了测试,执行的测试的顺序就不是字典式的

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.FixMethodOrder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.runners.MethodSorters;

import java.util.List;
@TestInstance(Lifecycle.PER_CLASS)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public abstract class ListTest {

  protected abstract List<Integer> provideList();

  private List<Integer> list;

  @BeforeAll
  public void setUp() {
    list = provideList();
    for (int i = 0; i < 100; i++) {
      list.add(i);
    }
  }

  @Test
  public void test1ShouldReturnActualSizeWhenListIsNotEmpty() {

    //when
    int actual = list.size();

    //then
    assertEquals(100, actual);
  }


  @Test
  public void test2ShouldReturnFalseWhenListIsNotEmpty() {

    //when
    boolean actual = list.isEmpty();

    //then
    assertEquals(false, actual);
  }

  @Test
  public void test3ShouldReturnTrueWhenListContainsElement() {

    //when
    boolean actual = list.contains(4);

    //then
    assertEquals(true, actual);
  }

  @Test
  public void test4ShouldReturnFalseWhenListDoesNotContainElement() {

    //when
    boolean actual = list.contains(200);

    //then
    assertEquals(false, actual);
  }

  @Test
  public void test5ShouldReturnGivenListAsArray() {

    //when
    Object[] actual = list.toArray();
    Object[] expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
        44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
        67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
        90, 91, 92, 93, 94, 95, 96, 97, 98, 99};

    //then
    assertArrayEquals(expected, actual);
  }
}
import static org.junit.jupiter.api.Assertions.assertarayequals;
导入静态org.junit.jupiter.api.Assertions.assertEquals;
导入org.junit.FixMethodOrder;
导入org.junit.jupiter.api.BeforeAll;
导入org.junit.jupiter.api.Test;
导入org.junit.jupiter.api.TestInstance;
导入org.junit.jupiter.api.TestInstance.Lifecycle;
导入org.junit.runners.MethodSorters;
导入java.util.List;
@测试(每类生命周期)
@FixMethodOrder(MethodSorters.NAME\u升序)
公共抽象类列表测试{
受保护的摘要列表提供列表();
私人名单;
@以前
公共作废设置(){
list=provideList();
对于(int i=0;i<100;i++){
列表.添加(i);
}
}
@试验
public void test1在存在诱惑时应返回实际值(){
//什么时候
int实际值=list.size();
//然后
资产质量(100,实际);
}
@试验
public void test2在存在诱惑时应返回false(){
//什么时候
布尔实际值=list.isEmpty();
//然后
资产质量(虚假、实际);
}
@试验
public void test3在ListContainesElement()时应返回True{
//什么时候
布尔实际值=列表。包含(4);
//然后
资产质量(真实、实际);
}
@试验
public void Test4在不包含元素()时应返回False{
//什么时候
布尔实际值=list.contains(200);
//然后
资产质量(虚假、实际);
}
@试验
公共无效测试5应返回givenlistasarray(){
//什么时候
Object[]实际值=list.toArray();
Object[]预期={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99};
//然后
资产质量(预期、实际);
}
}
下面是我执行的测试的顺序。无论我如何更改测试方法的名称,它始终保持不变:


我在哪里犯了错误?我也尝试过在抽象类上使用注释,而不是在从抽象类继承的类上使用注释,但也没有任何帮助

您正在合并Junit4和Junit5。当前Junit5不支持方法的顺序:


您只需使用Junit4即可。

您确定这是它们运行的顺序,而不是结果显示的顺序吗?此外,为什么您希望测试必须按特定顺序运行?@AndyTurner当我在debug中检查它时,例如,Test1ShouldReturnActualSizeWhenListson诱惑()应该首先执行,但它已经显示了应该添加到测试7中的元素