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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:47:21  来源:igfitidea点击:

JUnit - How to test a method with different values?

javaunit-testingjunitjunit4

提问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)
}
}