2

I'm trying to parameterize a JUnit4 test from a queue previously set in one of my src/main classes. This is what I've done so far, there's a class for the test suite (MigratorTestSuite)

@RunWith(Suite.class)
@Suite.SuiteClasses({ParameterizedTest.class})
public class MigratorTestSuite {
    @BeforeClass
    public static void setUp() throws SAXException, ParserConfigurationException, GitAPIException, IOException {
        Migrator.getReady();
    }

@AfterClass
public static void tearDown() throws SQLException {
    DatabaseManager.closeConnections();
    RepositoryManager.closeRepository();
}

}

And a class ParameterizedTest where I'm figuring out how to run a parameterized JUnit test which looks as follows:

@RunWith(Parameterized.class)
public class ParameterizedTest {

    @Parameterized.Parameters(name="whatever")
    public static Queue<Deque<String>> data(){
        return TestCasesConstructor.testCasesQueue;
    }

    private Deque<String> scenario;

    public ParameterizedTest(Queue<Deque<String>> q){
        scenario = q.peek();
    }

    @Before
    public void initialize() throws ParserConfigurationException, IOException, SQLException, ClassNotFoundException {

        System.out.println("--- Preparing database for running scripts");
        DatabaseManager.dropDatabase();
        DatabaseManager.createDatabase();
    }

    @Test
    public void testPlainMigration() throws Exception {
        Assert.assertTrue(Migrator.runScenario(this.scenario));
    }

    @After
    public void after() throws SQLException {
        DatabaseManager.closeConnections();
        TestCasesConstructor.testCasesQueue.remove();
    }
}

When I perform a mvn clean install test -Dtest=MigratorTestSuite the result is that it doesn't find any test and when I debug it, it shows:

No tests found matching data with any parameter from org.junit.runner.Request at org.junit.internal.requests.FilterRequest.getRunner

What am I doing wrong? Should I better implement it in TestNG? I'm really new to Junit.

Community
  • 1
  • 1
Nana89
  • 432
  • 4
  • 21

2 Answers2

1

You can try this :

mvn clean install -Dtest=MigratorTestSuite test 
Niks
  • 175
  • 7
1

The data method should return a collection of object arrays, i.e., Collection<Object[]>. Each object array is a test vector, containing, e.g., an input String and an expected outcome.

The test class constructor should take as arguments the elements of the test vector. In the example above, it would take a String and an outcome argument, which you would typically store in a field, so that the actual test case can use it. For each test vector, a new instance of the test class is created.

In your case, it seems you want to use a series of scenarios as parameters.

Therefore, your data method should return a collection of scenario-arrays (with each array of length 1), and your constructor should consume a single scenario (instead of the full collection; so there is no need to 'peek' around).

avandeursen
  • 8,458
  • 3
  • 41
  • 51