Rostislav V
Rostislav V

Reputation: 1798

JUnit Parameterized alternative for test method

I'm looking for a way to parameterize my test methods. I'm using JUnit and I want to find a way to unit test a couple of methods. With TestNG's DataProvider I can easily do that: I can provide scenarios to test some methods within a test class. With JUnit's Parameterized alternative I can do it, but the scope of it is the whole class, but I need to reduce the scope to one test method. Is there a way for that?

Upvotes: 2

Views: 1135

Answers (1)

Gorazd Rebolj
Gorazd Rebolj

Reputation: 825

Have you considered JUnit 5? It's got much better support for parameterization, including what you're looking for.

@ParameterizedTest
@ValueSource(strings = { "Hello", "JUnit" })
void test(String word) {
    assertNotNull(word);
}

See https://blog.codefx.org/libraries/junit-5-parameterized-tests/

Upvotes: 4

Related Questions