问题
I have a method and wish to test it with different values. My question is: how can I write a JUnit test that would test the same method with different values?
回答1:
You can take a look at parametrized tests like in example.
You can also use theories which is more convenient in a lot of cases.
回答2:
JUnit4 supports parameterized tests for just this purpose.
See this tutorial.
回答3:
I suggest you create a different unit test for each one of your (overloaded) function definitions, because arguably you are testing in fact different functions. For instance:
class MainClass {
public void method( int param) {... }
public void method( String param) { ...}
}
class MainClassTest {
@Test
public void methodIntTest() {
//call method(int)
}
@Test
public void methodStringTest() {
//call method(String)
}
}
来源:https://stackoverflow.com/questions/5794036/junit-how-to-test-a-method-with-different-values