Junit Testing
Junit Testing
Junit Testing
JUNIT TESTING
JUNIT TESTING
To implement the testing concept using Junit Testing
TESTING
DEBUGGING
RECOMMENDED IDE
NetBeans IDE
Eclipse IDE
1
I. JUNIT TESTING FOR SIMPLE ARTHMETIC CALC APPLICATION
MAJOR FUNCTIONS
1. add()
2. mul()
3. sub()
4. div()
1. SOURCE CODE
(Jcalc.java)
// mul method
public int mul(int a, int b)
{
return(a*b);
}
// sub method
public int sub(int a, int b)
{
return(a-b);
}
// div method
public int div(int a, int b)
{
return(a/b);
2
}
}
2. SNAPSHOTS FOR ADDING A TEST SOURCE CODE FOR EXISTING SOURCE FILE
3
3. CREATING A TEST CLASS IN NETBEANS IDE
4. TEST CLASS
(JCalcTest.java)
import org.junit.*;
// Test Class
@Test
System.out.println("add");
4
int a = 100;
int b = 300;
// expected result
// testing evaluation
assertEquals(expResult, actualresult);
@Test
System.out.println("mul");
int a = 0;
int b = 0;
// expected result
int expResult = 0;
// testing evaluation
assertEquals(expResult, result);
@Test
System.out.println("sub");
5
int a = 0;
int b = 0;
// expected result
// testing evaluation
assertEquals(expResult, result);
@Test
System.out.println("div");
int a = 500;
int b = 50;
// expected result
// testing evaluation
assertEquals(expResult, result);
6
5. PROJECT FOLDER SNAPSHOTS
7
6. OUTPUT
6.1 SUCCESSFUL TESTING EXECUTION
8
6.2 FAILURE TESTING EXCUTION
9
II. EXAMPLE OF TESTING ATM SYSTEM USING JUNIT-TESTING
APPLICATION : ATM
MAJOR FUNCTIONS
1. SOURCE CODE
(Atm.java)
10
if(amt>=100)
{
bal+=amt;
flag=true;
}
return flag;
}
// Withdrawal Module
public boolean Withdrawl(int amt)
{
flag=false;
// Two Test cases: (i) withdrawl amt must be 100 minimum. (ii) minimum balance must be
>=500 always
if(((bal-amt)>=500)&&(amt>=100))
{
bal-=amt;
flag=true;
}
return flag;
}
// Password Change Module
public boolean ChangePaaaword(String newPassword)
{
flag=false;
// Test case: (i) new password must not match old password
if(newPassword.equals(user)==false)
{
pwd=newPassword;
flag=true;
}
return flag;
}
}
2. TEST CLASS
(AtmTest.java)
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class AtmTest {
11
@Test
public void testLogin() {
System.out.println("Login");
String u = "sourav"; // user name
String p = "pugazh"; // password
Atm log = new Atm();
boolean expResult = true;
// test execution
boolean result = log.Login(u, p);
// testing evaluation
assertEquals(expResult, result);
}
@Test
public void testDeposit() {
System.out.println("Deposit");
int amt = 50;
Atm obj = new Atm();
boolean expResult = true;
// test execution
boolean result = obj.Deposit(amt);
// testing evaluation
assertEquals(expResult, result);
}
@Test
public void testWithdrawl() {
System.out.println("Withdrawl");
int amt = 200;
Atm instance = new Atm();
boolean expResult = true;
// test execution
boolean result = instance.Withdrawl(amt);
// testing evaluation
assertEquals(expResult, result);
}
@Test
public void testChangePassword() {
System.out.println("Change Password");
String newPassword = "pugazh";
Atm pw = new Atm();
boolean expResult = true;
// test execution
boolean result = pw.ChangePassword(newPassword);
// testing evaluation
12
assertEquals(expResult, result);
}
}
13
4. OUTPUT
4.1 SUCCESSFUL TESTING EXCUTION
14
4.2 FAILURE TESTING EXCUTION
15