gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:47:55 24 4
gpt4 key购买 nike

在测试我自己的 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);
}
}

这是我执行的测试的顺序。无论我如何更改测试方法的名称,它始终保持不变:

Test Results

我哪里搞错了?我还尝试不在这个抽象类上使用注释,而是在从它继承的类上使用注释,但它也没有帮助。

最佳答案

您正在组合 Junit4 和 Junit5。目前Junit5不支持方法顺序:https://github.com/junit-team/junit5/issues/13 .

您只需要使用 Junit4。

关于java - 名称升序 @FixMethodOrder 不适用于 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48068801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com