Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Junit Testing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

6.

JUNIT TESTING

JUNIT TESTING
To implement the testing concept using Junit Testing

TESTING

 Process of finding errors in given application

DEBUGGING

 Process of finding errors and Correcting errors in given application

RECOMMENDED IDE

 NetBeans IDE

 Eclipse IDE

INSTRUCTIONS TO THE CODER (CODING CONVENTION)

1. Name of the test class must end with "Test".

2. Name of the method must begin with "test".

3. Return type of a test method must be void.

4. Test method must not throw any exception.

5. Test method must not have any parameter.

1
I. JUNIT TESTING FOR SIMPLE ARTHMETIC CALC APPLICATION

APPLICATION : SIMPLE CALC

APPLICATION TYPE : JAVA CONSOLE APPLICATION

TYPE OF TESTING : UNIT TESTING (WHITE BOX)

IDE : NETBEANS 7.3.1

CONTENTS : 1 LOGIC FILE, 1 TESTED FILE

FRAMEWORK : JUNIT 1.4.10

MAJOR FUNCTIONS

1. add()

2. mul()

3. sub()

4. div()

1. SOURCE CODE
(Jcalc.java)

public class Jcalc {


// add method
public int add(int a, int b)
{
return(a+b);
}

// 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.*;

import static org.junit.Assert.*;

// Test Class

public class JcalcTest {

@Test

public void testAdd() {

System.out.println("add");

4
int a = 100;

int b = 300;

// creating an object for input source code

Jcalc obj = new Jcalc();

// expected result

int expResult = 400;

// actual result: testing execution

int actualresult = obj.add(a, b);

// testing evaluation

assertEquals(expResult, actualresult);

@Test

public void testMul() {

System.out.println("mul");

int a = 0;

int b = 0;

// creating an object for input source code

Jcalc obj = new Jcalc();

// expected result

int expResult = 0;

// actual result: testing execution

int result = obj.mul(a, b);

// testing evaluation

assertEquals(expResult, result);

@Test

public void testSub() {

System.out.println("sub");

5
int a = 0;

int b = 0;

// creating an object for input source code

Jcalc ss = new Jcalc();

// expected result

int expResult = 50;

// actual result: testing execution

int result = ss.sub(a, b);

// testing evaluation

assertEquals(expResult, result);

@Test

public void testDiv() {

System.out.println("div");

int a = 500;

int b = 50;

// creating an object for input source code

Jcalc obj = new Jcalc();

// expected result

int expResult = 10;

// actual result: testing execution

int result = obj.div(a, b);

// 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

APPLICATION TYPE : JAVA CONSOLE APPLICATION

TYPE OF TESTING : UNIT TESTING (WHITE BOX)

IDE : NETBEANS 7.3.1

CONTENTS : 1 LOGIC FILE, 1 TESTED FILE

FRAMEWORK : JUNIT 1.4.10

MAJOR FUNCTIONS

1. Login() : testing user name & password correctly

2. Deposit() : testing valid deposit amount

3. Withdrawal() : testing valid withdrawal amount

4. Change Password() : testing valid new password

1. SOURCE CODE
(Atm.java)

public class Atm {


static public int bal=5500;
boolean flag=false;
// Default user name and password
static String user="sourav",pwd="pugazh";
// Login Module
public boolean Login(String u, String p)
{
flag=false;
// Test case: (i) user name & password must be equal to default user name and password
if((user.equals(u))&&(pwd.equals(p)))
{
flag=true;
}
return flag;
}
// Deposit Module
public boolean Deposit(int amt)
{
flag=false;
// Test case: (i) Deposit amount must be >=100

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);
}
}

3. ATM PROJECT FOLDER SNAPSHOTS

13
4. OUTPUT
4.1 SUCCESSFUL TESTING EXCUTION

14
4.2 FAILURE TESTING EXCUTION

15

You might also like