当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重新生成测试时,它们就会被重写。
发布于 2011-11-17 16:22:50
我对CodePro不是很了解,但是看看JUnit Test Case Generation - Execution
为了确定目标方法的预期结果,代码生成器执行该方法。当方法的执行抛出异常时,CodePro > JUnit > Test Execution首选项控制代码生成器的响应。
It 看起来像是你的代码正在由CodePro执行,但是它抛出了一个NullPointerException,可能是因为设置没有正确完成?
CodePro正在生成两个测试用例,因为代码有两个路径通过它,但是NullPointerException意味着不会生成不同的测试代码。
我不完全理解所有涉及到的机制,但是尝试用一个只返回"“的方法替换retrieveCategoryId()并重新生成测试。如果这行得通,那么这就是问题所在。不过,我不知道解决方案是什么。在google codepro的论坛上试试。
发布于 2011-11-18 11:08:36
如果您希望自定义测试并防止它们被重写,请删除@generatedBy标记。这是对代码生成器的一个提示,即它拥有该方法,并且可以在需要时重写它。
发布于 2012-12-21 16:27:50
使用多个测试方法来测试其中一个方法是可以的。GooglePro尝试为您的方法的参数生成不同的值,然后使用这些值的组合创建一个测试方法。
您可以(自动)生成工厂类来帮助GooglePro获取这些值。在你的例子中,如果没有找到,它会用字符串的"“值和新的Category("")填充方法,因为你没有使用工厂类。
您可以在window > preferences > codePro > Junit > methods > Generate中限制每个方法的测试方法数量
这里有更详细的信息。JUnit Test Case Generation
https://stackoverflow.com/questions/8159946
复制相似问题