views:

291

answers:

12

Hi all:

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.

+5  A: 

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.

Georgy Bolyuba
I looked at the page. Seems pretty cool.
Robert Harvey
Unitils works with jUnit too
Jean-Philippe Caruana
+1 for mentioning Unitils. Interesting project I didn't know about before.
DaDaDom
A: 

I would go for JUnit.

Sujee
Why?...........
Robert Harvey
The newer versions of JUnit are rather feature rich. Almost everything that's available in TestNG is doable. So choosing a library is down to personal preference.
gpampara
Hi @Robert Harvey Because JUnit is is popular among developers and it is already used in several projects. When choosing a methodology, we should consider which one is good for the entire team not just one person. In that context, I think it is better to go for JUnit.
Sujee
+1  A: 

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

+2  A: 

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=

Shawn D.
@sdeer: oh, quite a huge gap as in Australia... never knew this :)
Michael Mao
+2  A: 

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.

Kurt Du Bois
+2  A: 

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

Dimitar
all you said is also true in jUnit
Jean-Philippe Caruana
I didn't think jUnit support parametric testing for objects
Dimitar
+1  A: 

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...

Jean-Philippe Caruana
+2  A: 

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.

Thorbjørn Ravn Andersen
TestNG is also supported by all the major IDEs.
Dan Dyer
As well as jUnit?
Thorbjørn Ravn Andersen
+2  A: 

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?).

Zenzen
A: 

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.

Sheetal Mohan Sharma
+10  A: 

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 ).

Cedric Beust
+1 for making my life easier in the past several years. :) You didn't mention data providers, which encourage and facilitate running the same test with different values.
Andy Thomas-Cramer
+1  A: 

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/

WStgo