测试应该很好地覆盖此类可能抛出的异常和错误的类型,并且应该很好地覆盖CalculatePrimesMother 的构造方法中的有缺陷的语句。
需要三个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();
}
}
}
最佳答案
您获得哪些保险?您的构造函数中没有 if 测试,因此单个调用应该执行我看到的所有代码。
你写了太多代码。 setUp、tearDown 和测试构造函数方法都是不必要的。删除它们。
您不需要在其他测试中使用 try/catch block 。把那些也去掉。您想要一个异常来触发测试失败。捕获将隐藏错误。
关于java - JUnit 测试用例的最大覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19411928/