Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Junit advantages

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to know the advantages of Junit. Using Junit does not mean that less code, right ?

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main advantage is that practically anyone who has done unit testing in Java has used JUnit. JUnit is so ubiquitous in that sense that I can't even name alternatives without Googling for them.

What do you mean by "less code"? If you consider the alternative "I'll write my own unit testing framework", then it's pretty obvious that JUnit means less code.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and the second major benefit is probably that all modern IDEs provide JUnit integration (running JUnit tests inside the IDE instead of launching a separate JUnit GUI or running a build script which executes the tests).
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To test some test case using JUNIT, I will have to write code , which can I very well do without JUnit. Junit uses some assertEquals method, which can be implemented easily. I dont see any advantage?

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:
...and the second major benefit is probably that all modern IDEs provide JUnit integration (running JUnit tests inside the IDE instead of launching a separate JUnit GUI or running a build script which executes the tests).


Yes, Eclipse integrates very well with JUnit.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
To test some test case using JUNIT, I will have to write code , which can I very well do without JUnit. Junit uses some assertEquals method, which can be implemented easily. I dont see any advantage?

The utility methods for asserting different things is not important. Most of the value provided by JUnit comes from the TestRunner which runs your tests, collects test results, produces XML output for further processing, etc.

It's not rocket science, but it's still work you don't need to do.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I felt most of its applicability in testing the business related classes and where we need to test the output of a server side algorithm etc.
(basically useful on server side with respect to J2EE realm).

But it is hands down the best unit testing tool for Java.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To test some test case using JUNIT, I will have to write code , which can I very well do without JUnit. Junit uses some assertEquals method, which can be implemented easily. I dont see any advantage?

Yes, but what does your code do? How does it let you know whether the tests are successful or not? Do you throw an exception, print output? Junit gives you a nice green bar. This provides great psychological value when coding. It also removes the possiblity that you will miss an error. Junit also runs the tests in isolation. Does your code do that?

Additionally as pointed out above, JUnit is a standard. If you ever decide to use Ant, you can integrate your tests into an automated build.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lasse and Jeanne for your replies.

Jeanne,

It also removes the possiblity that you will miss an error.


Could you please explain. I did not understand.

Junit also runs the tests in isolation.


What do you mean by isolation.


If you ever decide to use Ant, you can integrate your tests into an automated build


How do I do that?

Thanks Jeanne.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne: It also removes the possiblity that you will miss an error.
Pradeep: Could you please explain?

Jeanne's referring to the green bar, which is a clear visual sign of "no errors". It's a lot more difficult to miss a big red bar than it is to miss a single "FAILED" word and a non-zero error counter from a black'n'white console window.

Jeanne: Junit also runs the tests in isolation.
Pradeep: What do you mean by isolation?

Isolation means that the execution of one test does not affect the execution of another. Things get a bit tricky when you have static methods of which return values are mocked up differently by different tests, for example.

Jeanne: If you ever decide to use Ant, you can integrate your tests into an automated build.
Pradeep: How do I do that?

With the <junit> and <junitreport> tasks (see "optional tasks" in the Ant manual). There's a nice article at IBM developerWorks about running JUnit from an Ant script.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything Lasse said. I just want to add that the junitreport task generates a cool report. It shows you how many tests you have per class and project. And if there are any failures, it shows you where they are.
 
megha Ranjan
Greenhorn
Posts: 10
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep bhatt wrote:Hi all,

I want to know the advantages of Junit. Using Junit does not mean that less code, right ?



Hi Pradeep,

Junit is a way to test your code by writing different test cases for different scenarios. It is so popular because it can be implemented at any time during development to check the correct flow of the code and also logic. You can get all the information you need about junit from this link :

http://junit.sourceforge.net/doc/cookbook/cookbook.htm
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic