java - JUnit 测试用例的最大覆盖率

标签 java junit

测试应该很好地覆盖此类可能抛出的异常和错误的类型,并且应该很好地覆盖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/

相关文章:

java - 如何将 HttpServletRequest 对象传递给测试用例?

java maven如何使用捆绑的tomcat创建应用程序

java - 如何转换 PrimeFaces p :dataTable to standard h:dataTable (without skin) and then print it

java - JUnit 测试失败案例

java - 存储/记录/打印 errorcollector 收集的所有错误

rest - Dropwizard 测试 - javax.validation.ConstraintViolationException : The request entity was empty

Java Spring - 如何通过 @WithUserDetails 使用 application.properties 中的值

java - Wordpress API - 如何在 Android/Java 中循环对象的 JSON 数组

Java 代码跳过第一个 if 条件并跳过其他条件

java - Java和Python之间的套接字连接