jagamot
jagamot

Reputation: 5464

JUnit vs TestNg - For a New Project that using Spring MVC

Looking at the latest versions of JUnit[v 4.11] and TestNg[v 6.8.5] as of today, I see no big differences in both the frameworks.

If I am using Spring MVC in my project and have several layers and need unit/integration tests at all the controller / service / dao layers what framework would really be helpful for me.

I have prior experience working on JUnit but never used TestNg. Any advice?

Upvotes: 1

Views: 1282

Answers (3)

MostWanted
MostWanted

Reputation: 571

jUnit is great for unit testing, gets the job done well, however it was never built for integration testing, but in the last year or so it has been geared towards integration/automation testing.

TestNG from a very early stage was suitable for integration/automation testing, it has a more powerful version of dataprovider than jUnit, even before jUnit had dataprovider capabilities. It has much better annotation support than jUnit and unlike jUnit TestNG does not require your before/after class methods to be static. It has better multi thread/parallel execution support and has method dependency, as well as order execution. All of which jUnit needs or does poorly.

TestNG even has better reporting, please google for ReportNG or (my fav) TestNG-XLST.

if you want only simple unit testing, IMO go with jUnit, if you require a more robust test framework to meet your unit/integration/performance/automation testing, then I would very much recommend TestNG.

Upvotes: 4

Ittiel
Ittiel

Reputation: 1224

The difference isn't that big,

use the one you are familiar and comfortable with.

A little clarification:

With Junit 4.xx you can easily do almost anything that TestNG does (and you have the ability to easily extend it as well)

  1. Set the tests order: @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  2. Use parameters for test data: @RunWith(Parameterized.class)

or

@Rule public DataSetRule rule = new DataSetRule();

Upvotes: -2

Bob Dalgleish
Bob Dalgleish

Reputation: 8227

While you can do almost everything JUnit can do in TestNG, it takes a lot more work to have the facilities of TestNG in a JUnit environment.

One important facility required in integration testing, at an level, is to be able to order the tests you run, both for early bail out (it makes no sense to test application functionality when the database tests are failing) and for efficiency (having provisioning tests run before operational tests). While certain classes of unit tests (leaf tests) should be specified without order, almost all of the interesting stuff is done in non-leaf methods, with non-trivial setup.

Another area is parameterizing tests: TestNG has somewhat stronger handling of parameterization.

I can't present the benefits of JUnit over TestNG as the benefits of test ordering far outweigh not being able to order tests.

Upvotes: 2

Related Questions