August 15, 2005

Quick Fix for JUnit

The TestNG IDEA plug-in has an interesting feature that I just implemented in the Eclipse plug-in:  if you press Ctrl-1 (Quick Fix) on a JUnit test class, the plug-in will give you the option to convert it to TestNG, either with JDK5 annotations:

or JavaDoc annotations:

The operations performed by this extension are very simple:
  • Remove all the JUnit imports
  • Add TestNG imports (for JDK5 annotations only)
  • Import AssertJUnit
  • Remove extends TestCase
  • Add @Test annotations on top of any method that starts with "test"
  • Add @Configuration(beforeTestMethod = true) on top of setUp()
  • Add @Configuration(afterestMethod = true) on top of tearDown()
  • Add @Factory on top of suite()

And that's it!  These are fairly simple changes and again, I am struck at how intuitive the Eclipse API is.  Manipulating the AST is a real pleasure and as you can see above, the ASTRewriter gives you a lot of goodies for free, among which unlimited undo and a nice preview window.

 

Posted by cedric at August 15, 2005 08:44 AM
Comments

Can you do it on a whole package tree? Might make it a bit easier to convert a thousand-odd test cases.

Certainly, this sort of support would assist people looking to experiment with or migrate to TestNG. Quite a good idea.

Posted by: Robert Watkins at August 15, 2005 02:17 PM

Make it an ANT task. :-)

Posted by: Cameron at August 16, 2005 05:25 AM

Sir Otaku,

You give me Java project!

Posted by: Shitasam Suitam at August 16, 2005 07:12 AM

Still getting some of that "Sit Otaku" project request spam... good luck with that.

TestNG looks great, I was moderately happy with JUnit for many years, but since you have been pimping TestNG, my subconcious has come up with all sorts of problems with my junit code, and am feeling the need to bite the bullet. Damn you !

Things like this that ease the way are very very much appreciated.

Posted by: Michael at August 21, 2005 02:56 AM
Post a comment






Remember personal info?