好吧,我是第一次使用 JUnit。我的 ListOfFraction 类中有两个公共(public)函数:添加和删除。它们在此处的 ListOfFractionTest 类中列出,我要对此列表进行测试(这是一个大小为 2 的可增长列表)这就是我测试三个分数的原因。我对 add 的测试非常满意。但是我的删除方法实际上是在测试什么吗?我想有些问题是 JUnit 在我的场景中究竟会做什么来帮助我,我是否设置了合理的测试?请并感谢所有和任何建议。注意:::我只有大约 2 个月的 Java 知识。
/**
* Test of add method, of class ListOfFraction.
*/
@Test
public void testAdd()
{
System.out.println("add");
Fraction z = new Fraction(1, 2);
Fraction x = new Fraction(2, 3);
Fraction y = new Fraction("4/3");
ListOfFraction instance = new ListOfFraction();
instance.add(z);
instance.add(x);
instance.add(y);
// review the generated test code and remove the default call to fail.
boolean expResult = true; // we expect the result of delete to
// be true since we just added it.
boolean result = instance.delete(z);// Call actual delete
boolean result2 = instance.delete(x);
boolean result3 = instance.delete(y);
assertEquals(expResult, result); // Test the results.
assertEquals(expResult, result2);
assertEquals(expResult, result3);
}
/**
* Test of delete method, of class ListOfFraction.
*/
@Test
public void testDelete()
{
System.out.println("delete");
Fraction z = new Fraction(3, 2);
Fraction x = new Fraction(3, 3);
Fraction y = new Fraction("7/3");
ListOfFraction instance = new ListOfFraction();
boolean expResult = false;
boolean result = instance.delete(z);
boolean result2 = instance.delete(x);
boolean result3 = instance.delete(y);
assertEquals(expResult, result);
assertEquals(expResult, result2);
assertEquals(expResult, result3);
// TODO review the generated test code and remove the default
// call to fail.
}
}
最佳答案
你的测试很困惑:你的添加文本也在删除,然后测试它不存在,但如果添加和删除都被破坏(即什么都不做),你的测试仍然会通过!
最好先添加,然后确认已添加。在您的删除中,两者都可以执行,因为您正在单独测试添加部分。
此外,不要使用那样的变量。变化:
boolean expResult = true;
boolean result = instance.delete(z);
assertEquals(expResult, result);
简单地说:
assertTrue(instance.delete(z));
代码更少,更易于阅读,更易于调试。
关于java - 如何在 JUnit 中测试测试 Delete(),如何实际测试 J-Unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896509/