Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can we test a java code by using JUNIT?

Posted on 2011-02-27
7
Medium Priority
?
390 Views
Last Modified: 2012-05-11
Hi,

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..
0
Comment
Question by:haneef_nb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 34992382
Please go through this...

http://147.188.192.43/documentation/tutorials/docsystem/build/tutorials/junit/junit.html#JUnit-Introduction

it has step by step things on how to configure junit
0
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 34992396

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

http://vishnuagrawal.wordpress.com/2007/04/30/limitation-of-junit/

0
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 34992399
You can always plugin junit to your build tool (ant or maven), so that junit will run everytime you run the build.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 47

Accepted Solution

by:
for_yan earned 1336 total points
ID: 34992712

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







0
 
LVL 7

Assisted Solution

by:printnix63
printnix63 earned 664 total points
ID: 34993193
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 (http://www.junit.org)  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: (http://kentbeck.github.com/junit/javadoc/latest/)
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) {
   junit.textui.TestRunner.run(suite());
}

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 {
   @Test
   public void testSqr() {
      MySquare ms = new MySqare();
      double result = ms.MySqare(2.0);
      assertEquals(4,result,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.
 
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1336 total points
ID: 34993222
Thanks a lot for the explanation, now I more or less understand.
Hope, haneef will be also happy :)
0
 

Author Closing Comment

by:haneef_nb
ID: 35124632
Thanks..
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question