java JUnit - 如何测试具有不同值的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5794036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit - How to test a method with different values?
提问by user722781
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?
我有一个方法并希望用不同的值来测试它。我的问题是:如何编写一个 JUnit 测试来测试具有不同值的相同方法?
回答by Constantiner
You can take a look at parametrized tests like in example.
您可以查看示例中的参数化测试。
You can also use theorieswhich is more convenient in a lot of cases.
回答by skaffman
JUnit4 supports parameterized tests for just this purpose.
JUnit4 仅为此目的支持参数化测试。
See this tutorial.
请参阅本教程。
回答by Liv
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)
}
}