我不明白以下单元测试(来自我的教科书)如何充分测试Java的Math.min()
方法:
import java.util.Random;
/**
This app tests the method min of the class Math.
*/
public class MathTester
{
public static void main (String[] args)
{
final int VECTOR_SIZE = 100;
Random random = new Random();
boolean pass = true;
for (int i = 0; i < VECTOR_SIZE && pass; i++)
{
int a = random.nextInt();
int b = random.nextInt();
double min = Math.min(a, b);
if (a < min || b < min && (a != min && b != min))
{
System.out.print("The method failed the test case: ");
System.out.println("a = " + a + ", b = " + b);
pass = false;
}
}
if (pass)
{
System.out.println("The method passed all test cases");
}
}
}
假设a == 5
,b == 3
和min == 2
。这应该会导致失败,因为Math.min()
应该返回其较小的参数(并且2不是参数)。然而在我看来,测试会(错误地)传递这种情况,因为a
和b
都大于2,因此包含失败代码的if
块将不会执行