Two Laptop Bag*
The moose likes Testing and the fly likes Junit advantages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit advantages" Watch "Junit advantages" New topic
Author

Junit advantages

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi all,

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



Groovy
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
...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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jul 10, 2001
Posts: 1934
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.


Kishore
SCJP, blog
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

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

Joined: Jul 28, 2011
Posts: 10

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
 
jQuery in Action, 2nd edition
 
subject: Junit advantages