Junit

JUNIT Projects

JUNIT Project 1

JUNIT Examples

JUNIT EXAMPLE

adplus-dvertising
The Another Examples Of Testing Framework
Previous Home Next

The Simple example to methods we want to test using Junit.

  1. First getHello,return a String.
  2. getTrue, return a boolean.
  3. getElement and setElement, which throw exceptions.
package R4R;
public class samplecode 
{
private int[] array; 
public samplecode(int size)
{ 
array= new int[size]; 
} 
public String getHello() 
{ 
return  "Hello"; 
} 
public boolean getTruth()
{ 
return true; 
} 
public void setElement(int position, int value) 
throws ArrayIndexOutOfBoundsException 
{ 
array[position] = value; 
} 
public int getElement(int position) throws 
ArrayIndexOutOfBoundsException 
{ 
return array[position]; 
} 
}
How Can Using to Write a Testing Code?

The simplest way is as an expression in a debugger. If you can test the code then change debug expressions without recompiling, and you wait to decide what to write until you have seen the running the objects. then you write the program on test expression as statements which print to the standard output stream.

you using the JUnit, and you won't pollute the source code because test code is separated from buisness classes.

package R4R;
import junit.framework.*; 
import junit.textui.*; 
public class Testsamplecode 
extends TestCase { 
// samplecode instance used for tests 
protected samplecode samplecode; 
protected int testSize; 
// standard constructor 
public Testsamplecode(String 
name)
{ 
super(name); 
} 
/** 
* This method is called every time before
  particular test execution. 
* It creates new instance of tested class
  and it can perform some more
* actions which are necessary for tests. 
*/ 
protected void setUp()
{ 
testSize = 3; 
samplecode = new samplecode(testSize); 
} 
/** 
 * test original method getHelloWorld,
   example of Assert.assertEquals 
*/ 
public void testGetHello() 
{ 
System.out.println("Hello");
String result = "Hello"; 
Assert.assertEquals(result, samplecode.getHello()); 
} 
/** 
* test original method getTruth, example of 
  Assert.assertTrue 
*/ 
public void testGetTruth() 
{ 
System.out.println("True"); 
/* assertTrue checks if its parameter equals true,
   otherwise it throws an unchecked exception used
   within the framework to report on the error */ 
Assert.assertTrue(samplecode.getTruth()); 
} 
/** 
 * test original method setElement, example of Assert.fail 
 */ 
public void testSetElement() 
{ 
/* This test consists in checking that an exception is 
   throwed in method getException trying to access 
   element at position "size" of an array should 
   throw an ArrayIndexOutOfBoundsException. 
*/ 
try {
System.out.print("testSetElement, exception throwed :   -->  "); 
samplecode.setElement(testSize, 0); 
/*if exception is not catched, line Assert.fail is
  executed : test fails */ 
Assert.fail("setElement should raise an Exception"); 
} 
catch (Exception e) { System.out.println(e.toString()); } 
// test with valid parameter (no exception supposed to be throwed) 
try 
{ 
System.out.println("testSetElement, OK "); 
int position = testSize - 1; 
int result = 10; 
samplecode.setElement(position, result); 
Assert.assertEquals(result, samplecode.getElement(position)); 
} 
catch (Exception e) { System.out.println(e.toString()); } 
} 
/** 
* test original method getElement : to do ! 
*/ 
public void testGetElement() { 
} 
public static Test suite() { 
return new TestSuite(Testsamplecode.class); 
} 
public static void main(String[] args)
{ 
/* to use command line interface  */ 
junit.textui.TestRunner.run(suite()); 
// to use GUI interface (no Suite paramater permitted) 
} 
}

Output

You test the whole JavaProgram then Test result is out for

.testGetHello
sc.testGetTruth
.testSetElement, exception throwed :  
-->  java.lang.ArrayIndexOutOfBoundsException
testSetElement, OK 
.
Time: 0,015
There was 1 failure:
1) testGetHelloWorld(Testsamplecode)
junit.framework.AssertionFailedError: expected: but was:
at Testsamplecode.testGetHello(Testsamplecode.java:42)
at Testsamplecode.main(Testsamplecode.java:107)
FAILURES!!!
Tests run: 4,  Failures: 1,  Errors: 0

Previous Home Next