Question

Although I have programmed with Java for roughly 3 years + now (not day-to-day but at least I understand the fundamentals), haven't really come into the field of Unit Testing...

My work is now more Testing / Problem Analysis oriented, so I reckon a good Java Unit Testing Framework will be quite helpful to this role.

Obviously there is no formal rule of which framework to go with in my Team. So just wondering, as a beginner, which framework is a good one to get started quickly?

Junit?

TestNG?

or something else?

Edit: it seems JUnit has more printed books as references compared to TestNG on Amazon. But I don't know their popularity / trend in Java Industry as for now.

Was it helpful?

Solution

I'm the creator of TestNG and author of the "Next Generation Testing" book, so here are a few (obviously biased) thoughts.

For functional (or end to end / integration / system, whatever term you prefer) testing, TestNG offers a few features that JUnit doesn't have and that users have found very useful:

  • Groups. Once you have compiled your tests, you can just ask TestNG to run all the "front-end" tests, or "fast", "slow", "database", etc...

  • Support for multithreaded testing. This has two different meanings:

1) You can tell TestNG to run all your tests concurrently in thread pools that you define (one line change in an XML file). This is very configurable so that tests that are not multithreaded can still be run in single threads.

2) You can tell TestNG to invoke your test methods n times from p threads. This gives you a good idea of how thread safe the code you are testing is.

  • Test dependencies and deterministic ordering. This is very useful for integration testing where it's very common to see test methods that need prior test methods to succeed before they can even run. TestNG is very popular in the Selenium community for this specific reason ("don't bother testing this page of my web site if the login test failed").

Another immediate advantage of test dependencies is that TestNG can tell you "1 method failed, 99 methods skipped", which is a much more accurate result than "100 methods failed".

TestNG has many, many more features and I'm happy to answer questions here or on our mailing-list (http://groups.google.com/group/testng-users ).

OTHER TIPS

I would learn TestNG with Unitils. Covered all my needs all the time. Add XMLUnit and DBUnit and you should be settled for a long time.

To be perfectly honest I JUnit is way more popular than TestNG, at least here where I work and live. I know this might be a strange argument, but if I were you I'd scan through some job adds and see which framework does the industry in your area/country favour (for example I'm going for JUnit because of that, I mean sure I could learn TestNG, it might turn out to be better, but what good would it do to me if all the employers require JUnit knowledge?).

Although it may seem to be a petty criterion, all things being equals, it looks like job trends heavily favour JUnit.

http://www.indeed.com/jobtrends?q=TestNG%2C+JUnit&l=

Both do their jobs equally well, so there is no right or wrong. You can use both in combination with numerous frameworks to help support your development.

If you need to work for a client, you can learn the one that you are expected to use. If not, the best idea is to try them both out and find out what works best for you.

I've used both and found that for unit testing only jUnit works very well, but in terms of System testing I would recommend TestNG. TestNG offers a few extra features (like parametric testing) and is highly flexible. For example, in jUnit one test failing in a suite usually means you have to re-run your entire suite, but in TestNG you can simply rerun the test that failed. Heres a really good article outlining both (its a bit biased towards TestNG though)

http://www.ibm.com/developerworks/java/library/j-cq08296/index.html

JUnit is very well supported in many IDE's including Eclipse, and JUnit 4 with annotations is quite nice to work with.

Unless you have specific needs then go for tool support, i.e. jUnit.

TestNG with Unitils is really great. It covers almost all the requirements. Unitils support lot of assertion utils, Spring, Hibernate, mocking frameworks, dbutils etc.

To me, it is just a matter of taste. I've personnaly used jUnit daily for more than 5 years now and I'm very happy with it, especially with the last version and its @Rule feature that prevents my team from writting the same code over and over. It integrates well with all the tools I use daily : IDE, build, continuous intégration...

Junit is famous and best unit test tools, I think you and me both know Junit is so important so that Java developer changes their coding pattern as junit contribution, You can learn some Junit as basic unit test technology as well as learn testng as second unit test technology.

The following link maybe be useful for you! http://www.javarmi.com/2010/08/junit-4-tutorial-and-example-get-starting/

Junit is the best place to start. Junit 4.x has many rich features like annotations etc as well its get good support from popular frameworks like Spring.

I would go for JUnit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top