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

时间:2011-11-16 22:57:27

标签: 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);
}

这些是以下方法的测试:

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

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

3 个答案:

答案 0 :(得分:2)

我不太了解CodePro,但查看JUnit Test Case Generation - Execution

  

为了确定目标方法的预期结果,代码   生成器执行该方法。 CodePro&gt; JUnit&gt;测试执行   preferences执行时控制代码生成器的响应   方法抛出异常。

看起来像你的代码是由CodePro执行的,但它抛出了NullPointerException,可能是因为设置没有正确完成?

CodePro生成两个测试用例,因为代码有两条路径,但NullPointerException意味着没有生成不同的测试代码。

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

答案 1 :(得分:0)

如果要自定义测试并防止重写,请删除@generatedBy标记。这是代码生成器的一个提示,它拥有该方法,并且可以根据需要重写它。

答案 2 :(得分:0)

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

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

您可以在窗口&gt;中限制每种方法的测试方法数量。偏好&gt; codePro&gt; Junit&gt;方法&gt;最多生成

此处有更详细的信息。 JUnit Test Case Generation

相关问题