JUnit - How to test a method with different values?

无人久伴 提交于 2019-12-01 15:17:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!