RSS

JUnit Testcase Demo

12 Feb

Hi folks,

Here is the simple JUnit Testcase demo example with executed code.

Just follow the below steps to run and execute the below program to see the output.

Step-1 : Create a Java Project in eclipse IDE (version : 3.x)

Step-2 : Create a Java class with name “JUnitTestDemo” (no main method is required)

Step-3 : Download “junit.jar” file (URL :

Step-4 : Select Java Project and right click on it and select preferences. It will open properties dailog box, select Library Tab and add Junit jar file using “Add External jars…”

Step-5 : Put the below code in the JUnitTestDemo.java file

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class JUnitTestDemo {

// This Annotation will execute before the class starts execution
@BeforeClass
public static void beforeClass() {
System.out.println(“########################################### “);
}

// This Annotation will execute after the class execution
@AfterClass
// This method should be static otherwise it will throws exceptions
public static void afterClass() {
System.out.println(“########################################### “);
}

// This Annotation will execute before the method execution starts
@Before
public void beforeTestExecute() {
System.out.println(“——————————-“);
}

// This Annotation will igonre the below method execution
@Ignore
public void testAddNumber() {
if (addNumbers(10, 20) == 200) {
System.out.println(“<< Add method successfull >> “);
} else {
System.out.println(“<< Add method failed >> “);
fail(“:: Add method failed while adding numbers ::”);
}
}

// This Annotation will execute after the method execution completes
@After
public void afterTestExecute() {
System.out.println(“——————————-“);
}

// This Annotation will tells that execute this method once the JUnit starts executing
@Test
public void testMultipleNumber() {
if (multiplyNumbers(10, 20) == 200) {
System.out.println(“<< Multiply method successfull >> “);
} else {
System.out.println(“<< Multiply method failed >> “);
fail(“:: Multiple method failed while adding numbers ::”);
}
}

public int addNumbers(int a, int b) {
return a + b;
}

public int multiplyNumbers(int a, int b) {
return a * b;
}
}

Step-6 : Go to Run option at the top menu and select Run As –> JUnit Test option,
1. check ths console for output and also
2. check the JUnit show view to see the results.

Happy Coding!

Get glued for more updates.

Have a nice day.

Advertisements
 
Leave a comment

Posted by on February 12, 2013 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: