Why does google CodePro generate identical JUnit tests?

2.9k views Asked by At

When CodePro automatically generates tests for my methods, it often generates identical tests:

/**
 * 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);
}

These are tests for the following method:

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

Am I using CodePro wrong? I thought the multiple tests were hints for me to implement two tests, but whenever I customise the tests, they just get rewritten when CodePro regenerates the tests.

3

There are 3 answers

0
Matthew Farwell On BEST ANSWER

I don't know CodePro well, but looking at the JUnit Test Case Generation - Execution:

In order to determine the expected result of a target method, the code generator executes that method. The CodePro > JUnit > Test Execution preferences controls the code generator's response when the execution of a method throws an exception.

It looks like your code is being executed by CodePro but it is throwing a NullPointerException, probably because the setup isn't being done correctly?

CodePro is generating two test cases because the code has two paths through it, but the NullPointerException means that different test code isn't being generated.

I don't fully understand all of the mechanisms involved, but try replacing retrieveCategoryId() with a method which just returns "" and regenerating the test. If this works, then that is the problem. I wouldn't know what the solution is though. Try on the forums for google codepro.

0
Eric Clayberg - Google On

if you want to customize your tests and prevent them from being rewritten, remove the @generatedBy tag. That is a hint to the code generator that it owns that method and can rewrite it if needed.

0
Katu On

It's ok to have more than one test methods to test one of your methods. GooglePro is trying to generate different values for the arguments of your method and then creates a test method with combinations of those values.

You can (auto)generate factory classes to help GooglePro getting those values. In your case if it doesn't find any, it populates the methods with "" values for strings and new Category("") because you're not using a factory class.

You can limit the number of test methods per method in window > preferences > codePro > Junit > Methods > Generate at most

There is more detailed information here. JUnit Test Case Generation