在测试我自己的ArrayList实现时,我在测试之前设置了MyArrayList类的实例,并且为了检查我的逻辑是否在我使用@FixMethodOrder(MethodSorters.NAME_ASCENDING)的实现方法中应该如何工作。但是,一旦我运行我的测试,执行的测试的顺序就不是词典。
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);
}
}
以下是我执行的测试的顺序。无论我如何更改测试方法的名称,它始终保持不变:
我在哪里弄错了?我也尝试使用不在这个抽象类上的注释,而是从继承它的类,但它也没有帮助。
答案 0 :(得分:1)
您正在组合Junit4和Junit5。目前,Junit5不支持方法顺序:https://github.com/junit-team/junit5/issues/13。
您只需使用Junit4。