Java 有兴趣在单元测试中测试基本增量吗?
有兴趣用单元测试测试基本增量吗 测试类中的方法Java 有兴趣在单元测试中测试基本增量吗?,java,unit-testing,junit,Java,Unit Testing,Junit,有兴趣用单元测试测试基本增量吗 测试类中的方法 @Test public void testIncreaseString() throws Exception { } public void increaseString() { counterString++; } 原始类中的方法 @Test public void testIncreaseString() throws Exception { } public void increaseString() { coun
@Test
public void testIncreaseString() throws Exception {
}
public void increaseString() {
counterString++;
}
原始类中的方法
@Test
public void testIncreaseString() throws Exception {
}
public void increaseString() {
counterString++;
}
您的方法是公共的,并且它执行逻辑,因此测试它是有意义的 然而,这种方法
public void increaseString() {
counterString++;
}
不返回结果。所以要测试这个方法的行为,你应该检查这个方法的副作用
例如,如果您有一个方法
String getCounter()
,该方法返回counterString
的值,您可以使用此方法进行断言。您的方法是公共的,它执行逻辑,因此测试它是有意义的
然而,这种方法
public void increaseString() {
counterString++;
}
不返回结果。所以要测试这个方法的行为,你应该检查这个方法的副作用
例如,如果有一个方法
String getCounter()
返回counterString
的值,则可以使用此方法进行断言。counterString
不能是字符串,因为String
不支持递增运算符。我猜counterString
是int
counterString
不能是字符串,因为string
不支持递增运算符。我猜反字符串
是一个int
。