JUNIT TEST Case Sample for fresher
This is basic test case created using JUNIT. Which will give you basic understanding of Junit testcase .
/**
*
*/
package test;
/**
* @author PraveenKumar
*
*/
public class JunitTest {
public boolean validatePositivenonZero(int a, int b){
if(a > 0 && b > 0){
return false;
}
return true;
}
public int add(int a , int b){
return a+b;
}
public int multiply(int a, int b){
return a*b ;
}
public double add(double a, double b){
return a+b;
}
}
/**
*
*/
package test;
/**
* @author PraveenKumar
*
*/
public class JunitTest {
public boolean validatePositivenonZero(int a, int b){
if(a > 0 && b > 0){
return false;
}
return true;
}
public int add(int a , int b){
return a+b;
}
public int multiply(int a, int b){
return a*b ;
}
public double add(double a, double b){
return a+b;
}
}
/**
*
*/
package com.junit.testcase.test;
import static org.junit.Assert.*;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import test.JunitTest;
/**
* @author PraveenKumar
*
*/
public class UnitTestCase {
private int a;
private int b;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
a=0;
b=2;
System.out.println("Value has been set ");
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
System.out.println("Call at the end");
}
/**
* Test method for {@link test.JunitTest#validatePositivenonZero(int, int)}.
*/
@Test
public final void testValidatePositivenonZero() {
// fail("Not yet implemented"); // TODO
JunitTest ju = new JunitTest();
// assertEquals(true, ju.validatePositivenonZero(0, 5));
assertTrue(ju.validatePositivenonZero(0, 5));
assertFalse(ju.validatePositivenonZero(1, 5));
}
/**
* Test method for {@link test.JunitTest#add(int, int)}.
*/
@Test
public final void testAddIntInt() {
//fail("Not yet implemented"); // TODO
JunitTest ju = new JunitTest();
if(ju.validatePositivenonZero(0, 5)){
assertEquals(8, ju.add(3, 5));
}
}
/**
* Test method for {@link test.JunitTest#multiply(int, int)}.
*/
@Test
public final void testMultiply() {
//fail("Not yet implemented"); // TODO
JunitTest ju=new JunitTest();
Assert.assertEquals(6, ju.multiply(2, 3));
}
/**
* Test method for {@link test.JunitTest#add(float, float)}.
*/
@Test
public final void testAddFloatFloat() {
// fail("Not yet implemented"); // TODO
JunitTest ju=new JunitTest();
Assert.assertEquals(5.23, ju.add(2.03, 3.20));
}
}
Comments