Category Archives: JUnit

SIMPLE JUNIT EXAMPLE FOR BEGINNER THAT WORKS!!!

This Hello World program demonstrates basic features of JUnit.

All you need is:
1. Eclipse
2. JDK1.5
3. junit.jar (Download from http://www.junit.org)

In this program, usage of JUnit is demonstrated in order to test 2 components, namely:
1. Dice.java
2. DiceFactory.java

Test requirement for every new Dice created:
1. Playing a dice every time should get any number between (including) 1 to 6
2. A dice has 6 sides & no two sides should hold the same number.
3. Dice should be unbiased. In an unbiased dice, the sum of 2 opposite side numbers is always 7.
e.g. If back side is 6 then front side has to be 1.

Create Java project in eclipse.
NOTE: Make sure junit.jar is set in your classpath.

Folder structure
C\MyWorkspace\HelloJUnit:
│ .classpath
│ .project

├───bin
│ │ DiceTest.class
│ │
│ └───com
│ ├───mymodel
│ │ Dice.class
│ │
│ └───myservice
│ DiceFactory.class

├───src
│ └───com
│ ├───mymodel
│ │ Dice.java
│ │
│ └───myservice
│ DiceFactory.java

└───test
DiceTest.java

Create Dice.java
package com.mymodel;

import java.util.Random;

/**
* @author Anup
* A dice that has 6 sides, namely:<br>
* top, bottom, left, right, front & back.<br>
*/
public class Dice {

//Dice has six sides, each one carrying a number.
private int topSide = 0;
private int bottomSide = 0;
private int leftSide = 0;
private int rightSide = 0;
private int frontSide = 0;
private int backSide = 0;

/**
* Parameterised constructor.
* @param topSide
* @param bottomSide
* @param leftSide
* @param rightSide
* @param frontSide
* @param backSide
*/
public Dice(int topSide,
int bottomSide,
int leftSide,
int rightSide,
int frontSide,
int backSide) {
this.topSide = topSide;
this.bottomSide = bottomSide;
this.leftSide = leftSide;
this.rightSide = rightSide;
this.frontSide = frontSide;
this.backSide = backSide;
}

/**
* This method plays the dice to get a number.
* @return
*/
public int throwDice() {
return getRandomNumber();
}

public int getTopSide() {
return topSide;
}

public int getBottomSide() {
return bottomSide;
}

public int getLeftSide() {
return leftSide;
}

public int getRightSide() {
return rightSide;
}

public int getFrontSide() {
return frontSide;
}

public int getBackSide() {
return backSide;
}

/**
* Private method to get a random number.
* @return
*/
private int getRandomNumber() {
Random r = new Random();
int n = 0;
while (true) {
n = Math.abs(r.nextInt()) % 7;
if(n <7 && n >0)
return n;
}
}

}
Create DiceFactory.java

package com.myservice;

import com.mymodel.Dice;

/**
* @author Anup
* A factory class that deals with Dice object creation.
*/
public class DiceFactory {

/**
* Creates a dice object.
* @param top
* @param bottom
* @param left
* @param right
* @param front
* @param back
* @return
*/
public Dice getDice(
int top,
int bottom,
int left,
int right,
int front,
int back) {
return new Dice(top, bottom, left, right, front, back);
}

}
Create DiceTest.java
import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;

import com.mymodel.Dice;
import com.myservice.DiceFactory;

/**
* @author Anup
* A test case to unit test the Dice Program.
*/
public class DiceTest extends TestCase {

int top = 1;
int bottom = 6;
int left = 4;
int right = 3;
int front = 2;
int back = 5;
DiceFactory diceFactory = null;
Dice dice = null;

//Constructor
public DiceTest(String testMethodName) {
super(testMethodName);
}

//Initialise
public void setUp() {
diceFactory = new DiceFactory();
}

//Dice should not be null.
public void testGetDice() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
assertNotNull(dice);
System.out.println(“Dice created.”);
this.dice = dice;
}

//When dice thrown, should get any number between (including) 1 to 6
public void testThrowDice() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
int n = dice.throwDice();
assertTrue(n<7 && n>0);
System.out.println(“Dice number is between (including)1 & 6 = ” + n);
}

//Making sure NO two sides are same.
public void testUniquenessOfSides() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
Set set = new HashSet();//Set holds unique values.
String args[] = {
dice.getTopSide()+””,
dice.getBottomSide()+””,
dice.getLeftSide()+””,
dice.getRightSide()+””,
dice.getFrontSide()+””,
dice.getBackSide()+””
};
for(String i : args) {
assertTrue(set.add(i));
}
System.out.println(“No duplicates detected!”);
}

//Dice should be unbiased.
//In an unbiased dice, the sum of 2 opposite side numbers is always 7.
//e.g. If back side is 6 then front side has to be 1.
public void testUnbiased() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
assertEquals(dice.getFrontSide() + dice.getBackSide(), 7);
assertEquals(dice.getLeftSide() + dice.getRightSide(), 7);
assertEquals(dice.getTopSide() + dice.getBottomSide(), 7);
System.out.println(“Dice is unbiased.”);
}

//De-initialise
public void tearDown() {
diceFactory = null;
dice = null;
System.out.println(“Tear down <<<<“);
}

}

Execute the program
Wen you are done building project, right click on project.
Project –> Run As –> JUnitTest
There is no main method here.
That’s the whole idea. We have JUnit test methods instead.

Anup Jani
(Bsc, MCP, GNIIT, SCJP, SCWCD, IBM OOAD/UML JCert.)
My profile: http://www.linkedin.com/in/anupjani
My Blog: http://www.anupjani.wordpress.com