Recent Posts

RSS Feeds

TestNG is a leap beyond JUnit 3.8

Ok so so a little background first. I'm toying with testing the JSF runtime and wanted to run some multi-threaded tests. Everything I googled about JUnit said basically the same thing 'don't try this at home boys and girls'. But while googling I found numerous hits that TestNG had threading built into its nature. I remember when Cédric Beust first announced TestNG I thought I'd have to give that a shot but you know how things go, busy with this that or the other thing and it drops off the 'need to do' and into the 'someday I'll do that' pile. Anyway, TestNG fell into the 'today' pile last night while I was thinking about the multi-threaded test stuff for the JSF bits I wanted to test. So I downloaded the latest copy of TestNG (4.5.2) and the Eclipse plugin (4.5.0.4) and started playing around. One of the cool features in the Eclipse plugin is that you can convert your JUnit tests to TestNG tests with a Quick Fix (kind of funny that converting to TestNG is a fix :). So I clicked on my JUnit test and hit Command-1 (on Windows its Cntrl-1) and choose 'use annotations' and bada-boom bada-bing I've got TestNG tests. Quick painless and everything is working as it was before! I love it when a plan comes together.

This definitely passes the 10 minute test!

Now on to a quick tip - While playing with the multi threaded stuff I noticed that my ConcurrentAccessException was rarely happening with a few threads (3 to 6) and 20 to 30 runs of the test method. So I started poking around, turns out that 'invocationCount' causes all the invocations of the method to run in the same thread. Hmmm I thought perhaps if I put in additional methods they will all get their own thread too.

  @Test(groups={"threaded"}, invocationCount=7)
  public void testSetFactory1() {
    testSetFactory();
  }
  @Test(groups={"threaded"}, invocationCount=3)
  public void testSetFactory2() {
    testSetFactory();
  }
  @Test(groups={"threaded"}, invocationCount=5)
  public void testSetFactory3() {
    testSetFactory();
  }

  @Test(groups={"threaded"}, invocationCount=9)
  public void testSetFactory() {
    System.out.println("Thread.name = " + Thread.currentThread().getName());
    FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
        MockApplicationFactory.class.getName());
    Object factory = FactoryFinder
    .getFactory(FactoryFinder.APPLICATION_FACTORY);
    assert factory != null;
    assert factory instanceof ApplicationFactory;
  }

What do you know, problem solved. I later emailed back and forth with the google group and found that Cédric is going to be introducing a new feature soon that will make this even easier. One quick side note about Cédric, I don't think he sleeps! I was emailing back and forth with him at 12:30 am MST and I saw posts from him at 7:30am EST this morning. Caffeine must be your friend Cédric!

One other quick question/problem I ran into... When trying to use the Eclipse plugin to run groups I was not seeing my list of groups. Well turns out that you actually need to initialize the annotations with the correct type for things to work (go figure!) so anyway. I had code like this;

  @Test(groups="threaded", invocationCount=7)
  public void testSetFactory1() {
    testSetFactory();
  }
  @Test(groups="threaded", invocationCount=7)
  @ExpectedException(FooException.class)
  public void testSetFactory2() {
    testSetFactory();
  }

Turns out Eclipse allows this wrong info no problem (the groups and exception stuff must be arrays in case its not obvious at first glance), must be a bug in the incremental compiler. So when the plugin was looking for groups it could not find them because there was a type mismatch in creating them. Interesting to me because I could not find anyting in the eclipse log either ($workspace/.metadata/.log for inquiring minds).

Well that is all I have today for tips and tricks of TestNG, put in on your plate of stuff to learn about now and take it for a spin soon, you won't regret it.

In other news: I've not had a chance to play with JUnit 4.0 so there could be some really cool stuff in there that compares with TestNG. If you have played with it please comment, I'd love to hear others experience. And yes JUnit 4.0 is on my plate of stuff to learn about now :-)

Permalink     4 Comments



Comments:

TestNG is really great and easy to use. The reason I switch back to JUnit is tools support.

The IDEA plugin for TestNG made using IDEA almost impossible (IDE errors every few seconds) and JUnit support is already inlcuded in IDEA.

Posted by Lars Fischer on February 18, 2006 at 03:28 AM MST #

Lars, this is strange. I am using the IntelliJ IDEA EAP version 51xx and did not have any issue with regard to the TestNG plugin. Which version are u using?

Posted by CKNY on February 20, 2006 at 03:26 AM MST #

Bill,

it seems that IDEA 5.0.x caused problems when using the TestNG plugin. After Cédric mailed me privately I gave TestNG another try with the newer IDEA 5.1 version and the problems have vanished.

This is great news for me because I really like using TestNG. It looks like Jet Brains fixed a lot of errors and improved stability with IDEA 5.1.

Posted by Lars Fischer on February 20, 2006 at 03:40 PM MST #

CKNY, can you run TestNG plugin for IntelliJ Demetra (build 5131 and 5162) without exceptions?

I tried, it could run but have an exception relates with TestNG plugin UI.

Thanks!

Posted by t800t8 on February 28, 2006 at 12:43 AM MST #

Post a Comment:
  • HTML Syntax: Allowed