为什么谷歌CodePro生成相同的JUnit测试?

Kev*_*vin 5 java eclipse junit codepro

当CodePro自动为我的方法生成测试时,它通常会生成相同的测试:

/**
 * Run the String getCategoryID() method test.
 *
 * @throws Exception
 *
 * @generatedBy CodePro at 17/11/11 11:44 AM
 */
@Test
public void testGetCategoryID_1()
    throws Exception {
    Category fixture = new Category("");

    String result = fixture.getCategoryID();

    // add additional test code here
    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NullPointerException
    //       at java.io.StringReader.<init>(StringReader.java:33)
    //       at xpath.XPathRunner.<init>(XPathRunner.java:23)
    //       at trademefacade.Category.retrieveCategoryID(Category.java:95)
    //       at trademefacade.Category.getCategoryID(Category.java:68)
    assertNotNull(result);
}

/**
 * Run the String getCategoryID() method test.
 *
 * @throws Exception
 *
 * @generatedBy CodePro at 17/11/11 11:44 AM
 */
@Test
public void testGetCategoryID_2()
    throws Exception {
    Category fixture = new Category("");

    String result = fixture.getCategoryID();

    // add additional test code here
    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NullPointerException
    //       at java.io.StringReader.<init>(StringReader.java:33)
    //       at xpath.XPathRunner.<init>(XPathRunner.java:23)
    //       at trademefacade.Category.retrieveCategoryID(Category.java:95)
    //       at trademefacade.Category.getCategoryID(Category.java:68)
    assertNotNull(result);
}
Run Code Online (Sandbox Code Playgroud)

这些是以下方法的测试:

public String getCategoryID() throws IOException,
        NoCategoryMatchException {
    categoryID = retrieveCategoryID();
    if (categoryID.equals("")) {
        throw new NoCategoryMatchException();
    }
    return categoryID;
}
Run Code Online (Sandbox Code Playgroud)

我使用CodePro错了吗?我认为多个测试提示我实现两个测试,但每当我自定义测试时,它们只是在CodePro重新生成测试时被重写.

Mat*_*ell 2

我不太了解 CodePro,但看看JUnit 测试用例生成 - 执行

为了确定目标方法的预期结果,代码生成器执行该方法。CodePro > JUnit > 测试执行首选项控制方法执行引发异常时代码生成器的响应。

看起来您的代码正在由 CodePro 执行但它抛出 NullPointerException,可能是因为设置未正确完成?

CodePro 正在生成两个测试用例,因为代码有两条路径通过它,但 NullPointerException 意味着未生成不同的测试代码。

我不完全理解所涉及的所有机制,但尝试用仅返回“”的方法替换retrieveCategoryId()并重新生成测试。如果这有效,那就是问题所在。我不知道解决方案是什么。尝试访问 google codepro 论坛。