简体   繁体   中英

Executing TestNG tests with different parameters set in @BeforeClass method

I want to test some code with different persistence units. Therefor I have written two identical TestNG test classes that only differ in the name of the persistence unit that I pass to Persistence#createEntityManagerFactory to get the correct factory.

This call is in a setup method annotated with `@BeforeClass``

@BeforeClass
public void setupClass() {
    emf = Persistence.createEntityManagerFactory("test-eclipselink-h2");
    em = emf.createEntityManager();
    // init with some dummy data

    // ... some more initialization
}

What are the options to execute this test class with different persistence-units? It would be sufficient to have the names hardcoded in test classes, there is no need to specify them externally.

public class A {
    private String s;

    @DataProvider
    public Object[][] dp() {
        return new Object[][] {
          new Object[] { "test-eclipselink-h1" },
          new Object[] { "test-eclipselink-h2" }
        };
      }

    @Factory(dataProvider = "dp")
    public A(String s) {
        System.out.println("Creating test class with " + s);
        this.s = s;
    }

    @Test
    public void test() {
        System.out.println("Test " + s);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM