java - JUnit @Parameterized 函数在测试套件中的@BeforeClass 之前运行?

标签 java unit-testing junit automated-tests integration-testing

我正在使用 JUnit 测试套件运行一些测试,其中一个测试使用 @Parameterized 运行多次。我发现当我运行测试时,@Parameterized 函数在@BeforeClass 之前运行。这是预期的行为还是发生了其他事情?我原以为 @BeforeClass 会在任何测试开始之前运行。

这是我的测试套件:

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class TestSuite {

    @BeforeClass
    public static void setup() throws Exception {
        // setup, I want this to be run before anything else
    }

}

Test1 使用@Parameterized:

public class Test1 {

    private String value;

    // @Parameterized function which appears to run before @BeforeClass setup()
    @Parameterized.Parameters
    public static Collection<Object[]> configurations() throws InterruptedException {

        // Code which relies on setup() to be run first

    }

    public Test1(String value) {
        this.value = value;
    }

    @Test
    public void testA() {
        // Test  
    }
}

如何解决此问题以在运行其他任何东西之前运行 @BeforeClass setup() 函数?

最佳答案

不幸的是,这是按预期工作的。 JUnit在开始测试之前需要枚举所有的测试用例,对于参数化测试,使用@Parameterized.Parameters注解的方法来判断有多少个测试。

关于java - JUnit @Parameterized 函数在测试套件中的@BeforeClass 之前运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391202/

相关文章:

java - 在spring项目中添加aspect后出现UnsatisfiedDependencyException

java - NIO 和 IO 的管道有区别吗

ios - Xcode 5 : Unit Tests not running

ios - 将 Xcode 11 testresult .xcresult 转换为 JUnit?

java - 后缀概念

java - 如何检查值是否相等?

unit-testing - Nim 中的单元测试 : Is it possible to get the name of a source file at compile time?

java - 在同一对象中测试方法时如何模拟对象中的方法

java - 使用 stub 的 Spring JUnit 测试究竟如何工作?

java - Mockito、jUnit、可测试类都有@Named注解,测试时如何向其传递值