简体   繁体   English

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

[英]JUnit @Parameterized function is run before @BeforeClass in a test suite?

I am using a JUnit test suite to run a few tests, one of which is run multiple times using @Parameterized.我正在使用 JUnit 测试套件运行一些测试,其中一个测试使用 @Parameterized 运行多次。 I am finding that when I run my tests, the @Parameterized function is run before @BeforeClass.我发现当我运行测试时,@Parameterized 函数在@BeforeClass 之前运行。 Is this expected behavior or is something else happening?这是预期的行为还是发生了其他事情? I would have expected that @BeforeClass would run before any of the tests are started.我原以为 @BeforeClass 会在任何测试开始之前运行。

Here is my test suite:这是我的测试套件:

@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 uses @Parameterized: 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  
    }
}

How can I fix this to run the @BeforeClass setup() function before running anything else?我该如何解决这个问题,以便在运行其他任何东西之前运行 @BeforeClass setup() 函数?

This is, unfortunately, working as intended.不幸的是,这是按预期工作的。 JUnit needs to enumerate all of the test cases before starting the test, and for parameterized tests, the method annotated with @Parameterized.Parameters is used to determine how many tests there are. JUnit在开始测试之前需要枚举所有的测试用例,对于参数化的测试,使用@Parameterized.Parameters注解的方法来确定有多少个测试。

Although beeing a bit different solution, a static block does the trick.虽然 bee 有点不同的解决方案,静态块可以解决问题。 Also note, that it must be in the Test1.class.另请注意,它必须在 Test1.class 中。 But beside of that it works ;-)但除此之外,它还可以工作;-)

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

Recently ran into similar issue and solved problem using Function.最近遇到了类似的问题并使用函数解决了问题。 Example below.下面举例。

@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)));
    }
}

In your scenario, do database setup in @Before or @BeforeClass then inject into function在您的场景中,在 @Before 或 @BeforeClass 中进行数据库设置,然后注入函数

I found a hack to force a code segment to run before all other methods that are annotated with @Parameterized.Parameters .我发现了一个 hack 来强制代码段在所有其他用@Parameterized.Parameters注释的方法之前运行。 Just create a parameterized dummy test as follows:只需创建一个参数化的虚拟测试,如下所示:

@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();
  }
}

Then in your test suite, add this test first:然后在您的测试套件中,首先添加此测试:

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

Hoping this can help someone, I simply did it this way:希望这可以帮助某人,我只是这样做了:

    //@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() {
    //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JUnit 4:在测试运行之前在测试套件中设置东西(比如测试的@BeforeClass方法,仅​​用于测试套件) - JUnit 4: Set up things in a test suite before tests are run (like a test's @BeforeClass method, just for a test suite) 带参数的参数化测试或参数化套件 - A Parameterized Parameterized test or Parameterized Suite with a before and after junit spring-@BeforeClass在@ContextConfiguration之前运行 - junit spring - @BeforeClass run before @ContextConfiguration Java jUnit:在任何测试类之前运行的测试套件代码 - Java jUnit: Test suite code to run before any test classes 如何在每个@Test之前运行@BeforeClass方法 - How to run @BeforeClass method before each @Test JUnit 中位数参数化测试 function - JUnit parameterized test for median function 在JUnit 4中使用不同的@BeforeClass设置多次运行测试用例 - Run a test case multiple times with different @BeforeClass setups in JUnit 4 如何在 Eclipse 中运行通用参数化 JUnit 测试? - How to run a generic parameterized JUnit test in Eclipse? 您的测试套件是否需要 JUnit Engine 才能运行? - Is JUnit Engine required for your test suite to run? 如果作为测试套件的一部分运行,JUnit测试将失败 - JUnit tests fail if run as part of a test suite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM