测试应该能够很好地涵盖此类可以抛出的异常和错误的类型,并且它应该在CalculatePrimesMother的构造方法中覆盖有问题的语句。JUnit测试用例的最大覆盖范围
为三个JUnit测试案例所需的方法如下:
public CalculatePrimesMother(int numWorkers, int queueLength, int maxPrime,
boolean verbose) {
this.numWorkers = numWorkers;
// Instantiate 3 queues, for thread-safe communication with workers
Candidate = new ArrayBlockingQueue<Integer>(queueLength);
Prime = new ArrayBlockingQueue<Integer>(queueLength);
Composite = new ArrayBlockingQueue<Integer>(queueLength);
this.maxPrime = maxPrime;
this.sqrtMaxPrime = (int) Math.sqrt(maxPrime);
primeFactors = new int[sqrtMaxPrime];
this.verbose = verbose;
}
我试图创造一些测试情况,但没能获得全覆盖谁能帮助我?
public class CalculatePrimesMotherTest extends TestCase {
public CalculatePrimesMotherTest(String name) {
super(name);
}
private CalculatePrimesMother testMother;
@Test
public final void testCalculatePrimesMotherNegativequeueLength() {
try {
testMother = new CalculatePrimesMother(4, -12, 908, false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public final void testCalculatePrimesMotherMinusOne() {
try {
testMother = new CalculatePrimesMother(8, 12, 0, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
您的构造函数中没有分支,因此只需调用新的CalculatePrimesMother就可以完全覆盖构造函数方法。你用什么工具计算覆盖率?声称哪些行未被此工具覆盖? –
如果您没有得到全面覆盖,那必须是因为引发了异常,并且您正在测试中捕获它。这是你不应该做的事情。这个想法是让jUnit捕获代码中抛出的所有异常,以便它可以检测到测试是否失败。 – luanjot