views:

74

answers:

2

Hi there,

Not sure if this is possible, but it'll save me a lot of time if it is. When I create a Junit4 test case in Eclise, I don't usually included all the methods that I'd like to test first, and later on, I'd like to add the untested methods or new methods to the test case. Currently, I'm doing this by typing new test methods in the existing test case class. Is there a way for me to highlight the method name, and create a test case if not existed or add to an existing test case in Eclipse? Thanks in advance!

David

+1  A: 

No. But honestly, you shouldn't be having a one-to-one mapping of tests to methods anyway. I think the second page of the Eclipse "Create JUnit TestCase" dialogue is very badly designed because it kind of gives the impression that a one-to-one mapping is somehow useful.

You should have exactly as many tests as are required to fully test each method, and each test should only test one aspect of the method's behaviour. So if you need two or three or four tests, that's fine.

Which leads me to assert that the functionality you seek would not be hugely useful, because you will need to be adding lots more test methods anyway!

dty
+1  A: 

Not possible. Further, one method doesn't mean you have exactly one testcase for it. As @Danny has said, you will need to come up with several testcases to test every possible useful scenarios for that one method. If you combine all these test scenarios into one big testcase, then it is not going to be too useful. This is because when that testcase fails, you will have to dig deep into the testcase just to know which scenario fails, which is cumbersome.

To test one method, it is usually (if not, always) better to have many small testcases rather than one big testcase. When it fails, you know exactly which test scenario fails right away.

limc
thanks for both of your answers. I understand that I need to have many small testcases to one methods, and am not trying to do the one-to-one mapping. What I meant was, see you created a test method in a test case for method getMoney(), and now you want to add another test method for the same method. Currently, I need to do this by typing in the test case. Can I just highlight getMoney(), right click, create another test method in the test case if there is one exists. I guess I'm just too lazy here, :-)
David Zhao