简体   繁体   English

JUnit - 如何测试具有不同值的方法?

[英]JUnit - How to test a method with different values?

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测试,用不同的值测试相同的方法?

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. 你也可以使用在很多情况下更方便的理论

JUnit4 supports parameterized tests for just this purpose. JUnit4仅为此目的支持参数化测试。

See this tutorial . 请参阅本教程

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM