How can we test a java code by using JUNIT?


Please tell me how to test a java code by using junit in real time? what kind of code we can test with JUNIT?

Please give me the complete procedure..
Who is Participating?
for_yanConnect With a Mentor Commented:

No matter how many times I started  reading explanations about JUnit tests I could never figure out what it is all about.
It seems to me that all those explanations start from the strange place like piece of
code which begins

import junit.framework...

I would start explanation from the opposite end:
For example I have a program:

public class MySquare{

double x;
double sqr;

public MySquare(double x){
this.x = x;
sqr = x*x;


public double getSqr() {
return sqr;

public void static main(String [] args){
MySquare ms = new MySquare(new Double(args[0]).doubleValue());
System.out.println("square: " + ms.getSqr());


Assuming that  this piece compiles OK,
how would I go about starting testing it in JUnit?
I guess, I probably first need to download some jar with this junit stuff.
So what do I do next?

Perhaps haneef can now explain it to me :)

Please go through this...

it has step by step things on how to configure junit

Following gives you some insight on what are the limitations of junit...

Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

You can always plugin junit to your build tool (ant or maven), so that junit will run everytime you run the build.
printnix63Connect With a Mentor Commented:
Basically, JUnit is a framework that shall allow you to implement test cases as you write code, to do repeatedly, reliably and automatically tests on your program as it grows.

So, yes, the first step is, that you have to install the JUnit.jar (  into your projects compilation path and you will have to import the "test facilities" you want to use.
The API documentation is as well linked from there: (
You will have to implement the according test structures and assert if the assumption you make will be correct.

JUnit comes with several test examples that you can try out to get familiar,
like, you can run from your Junit-Install-Dir::

java -cp junit-4.6.jar;. junit.samples.AllTests

The JUnit example "AllTests class has a main method that will execute the sample tests:
public static void main(string [] args) {;

Each passed test produces a dot. Non passing tests will show an E for Error.
To work out all options you have to implement tests in your programs, there are books written about, which will blow the scope of this.

Basically, you have to import your wanted methods, so it look somewhat like this:

import static org.junit.Assert.*;
import org.junit.Test;
public class MySquareTest {
   public void testSqr() {
      MySquare ms = new MySqare();
      double result = ms.MySqare(2.0);
using your class above.

So from the directory your *.java file resides, you call
java -cp \junit4.6\junit-4.6.jar *.java
(assuming the junit-jar is in \junit4.6 and it is actually this version)
then you can try to run your  class by
java -cp .; \junit4.6\junit-4.6.jar or.junit.runner.JUnitCore.MySqareTest

I havent tested it exactly, but it shoud show the basic steps.
Hope it helps.
for_yanConnect With a Mentor Commented:
Thanks a lot for the explanation, now I more or less understand.
Hope, haneef will be also happy :)
haneef_nbAuthor Commented:
All Courses

From novice to tech pro — start learning today.