Java 为什么GoogleCodePro生成相同的JUnit测试?

Java 为什么GoogleCodePro生成相同的JUnit测试?,java,eclipse,junit,codepro,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

当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);
}

我是不是用错了CodePro?我认为多个测试对我来说是实现两个测试的提示,但是每当我定制测试时,当CodePro重新生成测试时,它们就会被重写。

我对CodePro不太了解,但是看看:

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

看起来您的代码是由CodePro执行的,但它抛出了一个NullPointerException,可能是因为安装没有正确完成

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


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

如果您想自定义测试并防止它们被重写,请删除@generatedBy标记。这是对代码生成器的提示,它拥有该方法,并且可以在需要时重写它。

可以使用多个测试方法来测试您的一个方法。GooglePro试图为方法的参数生成不同的值,然后用这些值的组合创建一个测试方法

您可以(自动)生成工厂类来帮助GooglePro获取这些值。在您的情况下,如果找不到任何,它将使用字符串和新类别(“”)的“”值填充方法,因为您没有使用工厂类

最多可以在窗口>首选项>codePro>Junit>方法>生成中限制每个方法的测试方法数

这里有更详细的信息。

您能发布这些测试的代码吗?
public String getCategoryID() throws IOException,
        NoCategoryMatchException {
    categoryID = retrieveCategoryID();
    if (categoryID.equals("")) {
        throw new NoCategoryMatchException();
    }
    return categoryID;
}