1

I have the following code, where each url in listOne is tested with the method testItem:

@Parameters(name="{0}")
public static Collection<Object[]> data() throws Exception {
    final Set<String> listOne = getListOne();
    final Collection<Object[]> data = new ArrayList<>();
    for (final String url : listOne) {
        data.add(new Object[] { url });
    }
    return data;
}

@Test
public void testItem() {
    driverOne.makeDecision(urlToTest);
    assertTrue(driverOne.success(urlToTest);
}

What if I now wanted to add a second list, listTwo, and run a test method defined as follows on JUST the items of listTwo (but not listOne?)

@Test
public void testItemAlternate() {
    driverTwo.makeDecision(urlToTest);
    assertTrue(driverTwo.success(urlToTest));
}

That is, I want driverOne to make the decision for all URLs in listOne, and I want driverTwo to make the decision for all URLs in listTwo. What is the best way to translate this to code? Thanks.

3 Answers 3

1

Cited from: https://github.com/junit-team/junit/wiki/Parameterized-tests

The custom runner Parameterized implements parameterized tests. When running a parameterized test class, instances are created for the cross-product of the test methods and the test data elements.

Thus, I assume No, that's not possible.

If you want to do such a thing I guess that you either

(1) will need to construct two test classes one for each test to be executed with the first collection and one for each test to be executed with the second collection or

(2) will need to use another mechanism besides the @Parameters annotation, maybe hand-crafted.

Sign up to request clarification or add additional context in comments.

Comments

0

You could include some test set identifier in your test set data itself, and then use the org.junit.Assume class to help:

@Test
public void testItem() {
    org.junit.Assume.assumeTrue(testSetId.equals("TEST_SET_1"));
    driverOne.makeDecision(urlToTest);
    assertTrue(driverOne.success(urlToTest);
}

@Test
public void testItemAlternate() {
    org.junit.Assume.assumeTrue(testSetId.equals("TEST_SET_2"));
    driverTwo.makeDecision(urlToTest);
    assertTrue(driverTwo.success(urlToTest));
}

Comments

0

As a completely different answer, there exists junit-dataprovider

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.