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



当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重新生成测试时,它们就会被重写。

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

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

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

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

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

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

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

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

您可以在window> preferences> codePro> Junit> methods> Generate中限制每个方法的测试方法的数量

这里有更详细的信息。生成JUnit测试用例

最新更新