2011-11-16 57 views
5

当的CodePro自动生成测试我的方法,它通常产生相同的测试:为什么谷歌CodePro生成相同的JUnit测试?

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

+0

你能发布这些测试的代码吗? –

+0

对不起,已完成。 – Kevin

回答

2

我不知道的CodePro很好,但是看一下JUnit Test Case Generation - Execution

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

看起来像你的代码正在被执行的CodePro但它抛出一个NullPointerException异常,可能是因为安装程序没有被正确呢?

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

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

0

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

0

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

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

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

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