10

Using Eclipse (Helios), I could create a JUnit test file ClassATest.java of the source file ClassA.java by using New -> JUnit Test Case -> Class under test..., then choose all the methods of ClassA to be tested.

If later we add some more methods to ClassA, how do we easily reflect this addition in ClassATest ? (No copy/paste plz).

CuongHuyTo
  • 1,333
  • 13
  • 19

2 Answers2

3

One solution is to use MoreUnit

With MoreUnit installed to Eclipse, one can right click onto the newly added method (and not yet unit tested), and choose "Generate Test"

Of course, if one always follows the writing-test-before-writing-method style, then this solution is not needed. However in reality sometimes you don't have a clear idea of what you would want to do, in that case you would have to code up some method, play with it, then rethink and code again until you are satisfied with the code and want to make it stable by adding unit test.

CuongHuyTo
  • 1,333
  • 13
  • 19
  • How MoreUnit can add a new test method for a newly created method in my class onto the existing junit test class? That Generate Test will generate a new test class but not a new test method on top of the existing test class. I am missing something? I am using Eclipse Luna – 5YrsLaterDBA Nov 10 '14 at 15:33
  • @5YrsLaterDBA: On Kepler it does generate a new test method in an existing test class. I'm seeing it with TestNG. Maybe the author of MoreUnit did not provide the release for Luna yet ? – CuongHuyTo Feb 13 '15 at 16:08
0

You should look into creating a JUnit test suite which will execute all tests within the classes you specify. Thus, adding new test cases is as simple as creating a new class and adding it to the @Suite.SuiteClasses list (as seen below).

Here's an example.

Example JUnit Test Suite Class:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    TestClassFoo.class
})
public class ExampleTestSuite {}

Example Test Case class:

public class TestClassFoo {
    @Test
    public void testFirstTestCase() {
        // code up test case
    }
}
uperez
  • 122
  • 3
  • 1
    Hi Uperez, my question is about updating the TestClassFoo automatically if there are NEW methods inside ClassFoo. – CuongHuyTo Dec 30 '13 at 14:05