首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么谷歌CodePro会生成相同的JUnit测试?

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

Stack Overflow用户
提问于 2011-11-17 06:57:27
回答 3查看 2.9K关注 0票数 5

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

代码语言:javascript
复制
/**
 * 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);
}

以下是对以下方法的测试:

代码语言:javascript
复制
public String getCategoryID() throws IOException,
        NoCategoryMatchException {
    categoryID = retrieveCategoryID();
    if (categoryID.equals("")) {
        throw new NoCategoryMatchException();
    }
    return categoryID;
}

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-17 16:22:50

我对CodePro不是很了解,但是看看JUnit Test Case Generation - Execution

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

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

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

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

票数 2
EN

Stack Overflow用户

发布于 2011-11-18 11:08:36

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

票数 0
EN

Stack Overflow用户

发布于 2012-12-21 16:27:50

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

您可以(自动)生成工厂类来帮助GooglePro获取这些值。在你的例子中,如果没有找到,它会用字符串的"“值和新的Category("")填充方法,因为你没有使用工厂类。

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

这里有更详细的信息。JUnit Test Case Generation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8159946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档