Home arrow Blog

JUnit vs TestNG a comparison

Since I launched this site, I have done a few tutorials on the two major unit testing frameworks in the Java space: JUnit and TestNG (you can view them in the Tutorials section of this site). Each of them are very competent frameworks that provide a robust set of features that allow developers to create complex test scenarios to adequately test just about any piece of code. But if you are starting a new project, which should you choose? If you already have a suite of test cases in one or the other, should you switch? Let’s find out.

Feature JUnit TestNG
User Defined Life Cycle check check
Test Organization (groups, etc) check
Distributed Test Execution check
Parallel Test Execution check
Data Driven Tests check
Dependency Testing check
IDE Integration check check
Ant Integration check check
Maven Integration check check
Domain Specific Extensions (Database, HTTP, etc) check
Active Community check check

The criteria I looked at, as you can see above was pretty diverse. User defined life cycle is the ability to tell the test runner what methods to execute in what order. Both JUnit and TestNG allow you to do this via annotations. The ability to organize my tests is another nice to have. TestNG allows you to group tests across test classes via annotations. This is a handy tool for crateing groups for different development tasks (checking in, continuous integration build, by functional area, etc) that JUnit doesn’t have. The most grouping you can do with JUnit is by grouping test methods together in a single class. On a large project the ability to run tests in either a distributed mode or in a parallel mode can definitely speed up execution. Yet another two points for TestNG. We have all come across very data intensive areas that require a large amount of data to truly test all scenarios. TestNG’s DataProvider method allows for the ability to build a data set for a single test scenario. The scenario is then executed over all of the data. Very powerful stuff. Dependency testing is also something that is important in a large system. I know these are both technically “unit test” frameworks, but they still should be able to handle all testing scenarios we throw at it. Given JUnit’s rigid enforcement of testing in isolation, it is difficult to test dependencies. Yet another point to TestNG.

Both frameworks integrate well into all major IDEs as well as the major build tools of the day. JUnit has a large collection of third party add ons that can help in domain specific areas like database testing and HTTP front end testing. However since TestNG can execute JUnit tests, you don’t loose the ability to use those tools by using TestNG. They both have an active community with a plethora of documentation available on the net (although at the time of this writing, JUnit.org has been down for a few days. Not sure what is going on there).

For me, the decision is clear. TestNG is a high caliber testing framework that can assist in just about any testing scenario you can think of. The guys over at TestNG did a good job. They originally intended to take JUnit and make it better. Given the easy learning curve and powerful features, it’s hard to argue against TestNG as the way to go in unit testing.

52 Responses to “JUnit vs TestNG a comparison”

  • kasper graversen responded:

    My understanding of TestNG is that it does not reload classes upon executing each test method. This was due to a lack of understanding of the Java language by the TestNG implementor. He even knew it was happening in Junit but apparently ignored that fact. And now so many people rely on the fact that classes won’t be reloaded, that he feels he can’t change testng…

    Stuff such as this concerns me to an extent that I’ve never picked up the framework. With regards to grouping tests with annotations, putting tests in different test folders have actually always sufficed for the stuff that we’ve been doing. such strategy doesn’t support multiple groupings of the same tests… but we’ve never felt the need for that.

    just my 2 cents
    kasper, http://firstclassthoughts.co.uk/

  • Hani Suleiman responded:

    I’m somewhat aghast that any Java developer would say that object should be reinstatiated on every method invocation, and that NOT having this behaviour is due to a misunderstanding of how Java works. Serious Kaspar, do you have a job doing Java stuff?

    Exactly how many people do this? Create an object, invoke a method on it, throw it away, create another instance, invoke a 2nd method, and so on?

  • Cedric responded:

    Hi Kasper,

    I don’t even know where to start :-)

    The motivations behind TestNG are very clearly explained in the documentation, in the multiple articles that have covered TestNG since its inception and, of course, in our book (”Next Generation Testing in Java”, check it out, you might find a few things that will challenge your thinking).

    And by the way, get your vocabulary straight: we are not talking about reloading classes but reinstantiating them.

    Let me give you a short summary of these reasons since you obviously didn’t bother researching any of this: keeping the values of the test objects between method invocations is not only intuitive to any Java developer (as Hani pointed above) but it’s also the only sane way to maintain expensive state across your tests (I’m betting you use static fields to achieve this?).

    Please check out some of the material I have written over the past years on the subject and I’ll be happy to discuss specific issues with you, as long as you refrain from using ad hominem attacks.


  • Wolter responded:

    I’m not sure that I understand the issue you guys are referring to.

    Are you saying that TestNG instantiates the objects to test only once, and then uses the same objects for all of the tests?

    If so, wouldn’t this cause unexpected dependencies between the current test and previous tests?

    I’m imagining some complex object that holds a number internally as part of its function. It’s a very magic number that gets changed when certain methods are called. This means that in the course of testing, the number will get changed depending on what tests are run and in what order. In a later test, you call addToNumber(int value), and then getCurrentNumber() to make sure it added correctly. However, if previous tests called methods that muck about with this number, how can you write an effective test for addToNumber short of reinstantiating the object?

  • Crias responded:


    I don’t know the history of TestNG, so I can’t comment, but the fact that JUnit has moved in same direction should sufficiently demonstrate the testing world’s inclination toward this practice. And for good reason.

    Example. setUp() takes 0.99s and sets up some variables that I have no intention to change. Each test takes 0.01s. For 1 test, this cost is annoying… but I’ll accept it. For 100 tests, repaying this cost each time is unacceptable.

    The trade-off is that you’re assuming that after a test you can just throw away the test instance and move on to a new one. In the old way, that was true, and it saved you writing a proper teardown(). So the cost of saving huge amounts of test time by proper separation is a little more effort to do proper setup and teardown via @Before and @After


    I believe you’re misunderstanding Kasper. He claimed TestNG’s creator had a lack of understanding of the Java language, not a lack of understanding of Java or OO paradigms.

    Essentially, he’s claiming the guy knew he “should” reinstantiate, but didn’t know how to. He doesn’t mean every object you ever create should only ever have a 1-function lifespan.

    (Note: I’m not agreeing with him, just clarifying his stance.)


    Statements like “since you obviously didn’t do your research” and “get your vocabulary straight” followed by “but don’t attack me” are clearly flaimbait. Trolling is lame dude, even if you do know your stuff.


    It depends on your testing methodology. There is a trade-off made during the switch.

    During JUnit 3.8 tests, the lifecycle is simplistic.
    foreach(Test) { Instantiate TestCase; setup(); test(); teardown(); Discard TestCase; }
    Because TestCase was being discarded, you could actually opt to skip teardown() completely in most cases.

    During JUnit 4 and TestNG tests, the lifecycle is different.
    { Instantiate; BeforeClass; foreach(test) { Before; Test; After; } AfterClass; Discard; }
    As a result, After (or teardown) becomes very important - you must clear out any dirty data. You must also make sure you put the right stuff into BeforeClass and Before - only put static unchanging setup into BeforeClass. However, as a result it makes the storage of expensive data a lot simpler.

  • http://room8brightwater.blogspot.co.nz/2013/08/term-3-writing-speeches.html responded:

    I think the admin of this web site is really working hard in
    support of his website, for the reason that here every material
    is quality based material.

  • Read the Full Article responded:

    I am regular visitor, how are you everybody? This post posted at this website is
    genuinely fastidious.

  • alturl.com responded:

    An impressive share! I have just forwarded this onto a colleague
    who had been doing a little homework on this.
    And he in fact bought me dinner due to the fact that I discovered
    it for him… lol. So allow me to reword this….
    Thank YOU for the meal!! But yeah, thanx for spending time to discuss this topic
    here on your web page.

  • choiceness N: choicenesses responded:

    I’d like to find out more? I’d want to find
    out some additional information.

  • http://wheretoshopseattle.com/?page_id=2 responded:

    Great blog you have here but I was curious about if you knew of any forums that cover the
    same topics discussed in this article? I’d really love
    to be a part of group where I can get responses
    from other experienced individuals that share the
    same interest. If you have any suggestions, please let me know.

  • Jaunita responded:

    It’s fantastic that you are getting thoughts from thjis article as
    well as from our argument made at this time.

  • Shaunte responded:

    Nice post. I learn something totally new and challenging on websites I stumbleupon everyday.
    It will always be interesting to read content from other authors and use a little something from their sites.

  • Cosmetic responded:

    It’s fawntastic that you are getting thoughts from this paragraph as well as from our
    dialogue made here.

  • Google responded:

    * A sitemap to navigate your website is helpful for visitors to access main pages.
    These pre-computed numbers, hold on in a very giant information bank for millions or URLs on the net.
    But it seems Memorial Day wasn’t important enough to Google.

  • zapatos novia responded:

    Ms bien iluminando agradezco, creo que sus suscriptores pueden seguramente querr mucho
    mass revisiones a llo largo de estas lneas sigan el buen lugar de visita

  • mccarthyvdsz.jigsy.com responded:

    Hello, I want to subscribe for this web site to take hottest updates, so where can i do it please help.

  • bestcrib mattress 2014 responded:

    If you want to take much from this paragraph then you have to apply such
    techniques to your won weblog.

  • Perception Sport Swifty 9.5 Kayak Spray Skirt responded:

    Good post however I was wanting to know if you could write a litte more on this topic?

    I’d be very thankful if you could elaborate
    a little bit further. Thank you!

  • Shantell responded:

    For more inspiring stories, check out the other profiles in Bright Hub’s Collection of Interviews
    and Biographies of Successful Entrepreneurs. Usually, these proclamations are backed up with
    a hearty helping of user statistics and impressive engagement and conversion rates.
    A dealership that does not use Internet marketing is more likely to lose all these important
    prospective buyers.

  • ssense canada goose responded:

    At this time it looks like Expression Engine is the best blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?
    ssense canada goose http://www.pineacrefarms.com/Scripts/canada-goose-outlet/@@ssense-canada-goose43bc.html

  • Ayur responded:

    It’s actually a nice and helpfull piece of information. I am happy
    that you just shared this helpful information with us.
    Please keep us up to date like this. Thanks for sharing.

  • New Age responded:

    whoah this blog is fantastic i like readingg your posts.

    Keep up the great work! You already know, a lot of people aree searching around for thjis information, you can aid them

  • Bradly responded:

    Exceloent post. Keep writing such kind of information onn your blog.
    Im really impressed by it.
    Hi there, You’ve dne an excellent job. I’ll certainly digg it and in mmy view recommend to my friends.
    I am confident hey will be benefited from this

  • i loved this responded:

    Thereafter you push the fasteners click here fit right.
    Product quality - Most Product Packaging Companies in California.
    Here is the number of templates are provided on the large type.

  • Hugh responded:

    Excellent article. I am experiencing some of thyese issues as well..

  • blog-fisip13.web.unair.ac.id responded:

    Quality posts is the important to invite the
    viewers to pay a visit the site, that’s what this web site is providing.

  • indieartistradio.net responded:

    But for live entainment the most compelling gameplay.

    For his third ineffective at bat though making it very well in advance, especially” Ye haave brought disaster tto your customers. Hey, live entainment you wanna wear your dirndl.

  • Santos responded:

    An amortization schedule is basically a table containing loan details.

    Devora Witts is a certified loan consultant with several
    years of experience in the credit area who instructs people regarding credit recovery and approval for personal loans, home loans, consolidation loans, car loans,
    student loans, unsecured loans and many other types of loans.
    Lenders are able to charge a fee after a certain number of days past your
    due date.

  • Novella responded:

    It’s nearly impossible too find experienced people on this
    subject, however, you seem like you know what you’re talking about!


  • wayne banks responded:

    What’s up mates, good paragraph and fastidious arguments commented
    here, I am genuinely enjoying by these.

  • sell your invoices responded:

    Normally I don’t learn article on blogs, but I would like to say that this
    write-up very compelled me to check out and do so!
    Your writing taste has been amazed me. Thank you, quite nice article.

  • Männer kennenlernen in Seekirchen am Wallersee Österreich responded:

    As the admin of this website is working, no doubt very soon it will be renowned, due tto its feature contents.

  • Osvaldo responded:

    Magnificent beat ! I would like too apprentice at the same
    time as you amend your site, how can i subscribe for a blog site?
    The account helped me a appropriate deal. I weere a little
    bit familiar of this your broadcast provided vibrant transparent

  • http://healthtipsntricks.com responded:

    Hello it’s me, I am also visiting this site daily, this web site is really pleasant and the visitors
    are in fact sharing good thoughts.

  • ace fishing generateur responded:

    you are actually a good webmaster. The web site loading velocity is incredible.

    It kind of feels that you are doing any distinctive trick.
    In addition, The contents are masterpiece. you’ve done a excellent activity on this matter!

  • school pa system responded:

    school pa system…

    See here for the top wireless public address system now in stock around and reasonably priced….

  • General Dentist Plantation Fl responded:

    There’s definately a great deal to know about this subject.
    I love all of the points you’ve made.

  • top pediatrician in Jacksonville fl responded:

    If you are going for best contents like me, just visit this site daily because
    it offers feature contents, thanks

  • www news in tamil responded:

    This web site certainly has all of the info I needed about this subject and
    didn’t know who to ask.

  • get a device fix responded:

    It’s really a great and helpful piece of info. I am happy that
    you just shared this helpful information with us.
    Please keep us up to date like this. Thank you for sharing.

  • Bodrum escort responded:

    Hello friends, its great paragraph about educationand completely defined, keep it up all the

  • Sexual Questions responded:

    Fascinating and stunning how your post is! It Is Useful and supportive for me That I like it all that much, and I am anticipating Hearing from your next..

  • Sexual Questions responded:

    I am exceptionally delighted in for this web journal. Its an instructive theme. It help me all that much to take care of a few issues. Its chance are so phenomenal and working style so fast.

  • robots txt responded:

    Hello, I discover perusing this article a delight. It is to a great degree accommodating and intriguing and all that much anticipating perusing a greater amount of your work..

  • Questions to ask your crush responded:

    I am glad to discover this post extremely valuable for me, as it contains part of data. I generally like to peruse the quality substance and this thing I found in you post. A debt of gratitude is in order for sharing.

  • Serious about you responded:

    Undoubtedly this is a phenomenal post I got a considerable measure of learning subsequent to perusing good fortunes. Subject of online journal is astounding there is just about everything to peruse, Brilliant post.

  • Brio Dental juarez prices responded:

    After I initially commented I appear to have clicked the -Notify me when new
    comments are added- checkbox and now whenever a comment is added I receive four emails with the
    same comment. Perhaps there is a way you are
    able to remove me from that service? Appreciate it!

  • web page responded:

    It’s an awesome article designed for all the internet viewers;
    they will get benefit from it I am sure.

  • okapy best responded:

    There is certainly a great deal to know about this topic. I like all of the points you made.

  • Infatuation Scripts responded:

    I might want to say this online journal truly persuaded me to do it! Much appreciated, great post.

  • Kenneth responded:

    I am Miss out on Philadelphia Tropic Caribbean Natural beauty Queen 2009-2010 I will be generating apperances all through our Great metropolis!

  • دانلود فیلم اکسیدان responded:

    You ought to take part in a contest for one of the greatest sites
    on the internet. I am going to highly recommend this web site!

Add your own comment...


    Twitter Button from twitbuttons.com