Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does google CodePro generate identical JUnit tests?

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.

like image 629
Kevin Avatar asked Nov 04 '22 11:11

Kevin


1 Answers

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.

like image 189
Matthew Farwell Avatar answered Nov 10 '22 18:11

Matthew Farwell