在测试套件中,JUnit @参数化函数在@BeforeClass之前运行?

nzrxty8p  于 2022-12-18  发布在  其他
关注(0)|答案(5)|浏览(178)

我正在使用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
    }

}

测试1使用@参数化:

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()函数?

oyt4ldly

oyt4ldly1#

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

vvppvyoh

vvppvyoh2#

虽然是一个有点different的解决方案,一个静态块就可以了。还要注意,它必须在Test1.class中。但是除此之外,它还可以工作;- )

@RunWith(Parameterized.class)
public class Test1 {

    static{
        System.out.println("Executed before everything");
    }

    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  
    }
}
iqjalb3h

iqjalb3h3#

最近遇到了类似的问题,并使用函数解决了问题。示例如下。

@RunWith(Parameterized.class)
public class MyClassTest {

    @Parameterized.Parameters
    public static Iterable functions() {
        return Arrays.<Object, Object>asList(
            param -> new Object()
        );
    }

    Object param;
    Function function;

    public MyClassTest(Function f) {
        this.function = f;
    }

    @Before
    public void before() {
        // construct dependency
        param = "some value";
    }

    @Test
    public void test() {
        assertThat(myClass.doSomething(function.apply(param)), is(equalTo(expectedValue)));
    }
}

在您的场景中,在@Before或@BeforeClass中进行数据库设置,然后注入函数

ikfrs5lh

ikfrs5lh4#

我发现了一个强制代码段在所有其他用@Parameterized.Parameters注解的方法之前运行的黑客攻击。

@RunWith(Parameterized.class)
public class DummyInitTest
{
  @Parameterized.Parameters
  public static Collection<?> constructorFeeder()
  {
    // Your setup here. This will run before anything else.
    // Return empty list so no tests will be executed for this test class.
    return ImmutableList.of();
  }
}

然后在测试套件中,首先添加以下测试:

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

qgelzfjb5#

希望这能帮助别人,我只是这样做:

//@BeforeClass does not run before Parameters annotation
    public static void beforeClassSetup() throws IOException {
       InputStream is = Test.class.getResourceAsStream("/TestData.json");
       // load data...
    }
       
    @Parameters(name = "testProductFunctionality")
    public static Collection<Object[]> data() throws IOException {
        beforeClassSetup();
        // create parameters...
     }

    @Test
    public void testProductFunctionality() {
    //...

相关问题