package dpackage;
public class MyCalculator {
public int getSum(int a, int b, int sum) {
sum = a+b;
return sum;
}
}
package dpackage;
import junit.framework.TestCase;
public class MyCalculatorTest extends TestCase {
MyCalculator calc = new MyCalculator();
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
protected void getSum(){
int a=2;
int b=3;
int sum = a+b;
assertEquals(5, calc.getSum(a, b, sum));
}
}
问问题
933 次
3 回答
6
原因有点微妙。将代码更改为以下以获得绿色条。
public void testGetSum()
test
在 Junit 3 中,测试方法应该从public
如果可能的话,我会建议你继续使用 Junit 4,它没有施加这样的限制。
使用诸如@Test
,之类的注解@Before
,@After
你的代码会更加简单易读。
同样传递sum
给该方法,然后重新计算它看起来是多余的。坚持用你的getSum
方法计算它。
于 2012-10-07T08:11:10.453 回答
3
您没有任何名称以“test”开头的方法。您可以在 MyCalculatorTest 类中将“getSum”方法重命名为“testGetSum”。
于 2012-10-07T08:11:22.350 回答
2
这个定义有什么意义?
public int getSum(int a, int b, int sum) {
sum = a+b;
return sum;
}
只需使用:
public int getSum(int a, int b) {
return a + b;
}
和
public void testGetSum() { <-- note test in front
int a=2;
int b=3;
assertEquals(5, calc.getSum(a, b));
}
注意eclipse没有在方法前面报错test
是没有检测到测试类。
于 2012-10-07T08:05:05.660 回答