Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need help testing the following Java code in Junit on netbeans. I am trying to do the white box path coverage testing that

Hello, I need help testing the following Java code in Junit on netbeans. I am trying to do the white box path coverage testing that detects errors on a specific path exececuted. Please help me test the following codes using the white box method and help me with the screenshots. Thank you for your help in advance

// account class

package BankAccount;

import static java.lang.Character.toUpperCase;

import java.util.Scanner;

import BankAccount.AccountState;

import BankAccount.Arrear;

import BankAccount.Empty;

import BankAccount.Sound;

public class Account {

//Account attribute balance; initialized to 0.0

double balance = 0.0;

AccountState state;

/**

* @param args the command line arguments

*/

public Account() {

this.setState(new Empty());

}

public static void main(String[] args) {

//Create object of Account

Account obj1 = new Account();

//amount used for altering balance

double amount = 0.0;

//Scanner Object

Scanner sc = new Scanner(System.in);

//Char choice; will allow user to repeat program

char choice = 'Y';

//Loop used for multiple iterations

while(choice != 'N')

{

//User Prompt for amount

System.out.println(" What is the amount that you would like to add to your balance? (It may be negative): ");

//Input Validation Loop

while(!sc.hasNextDouble())

{

System.out.println(" Error, please enter a valid double value: ");

sc.next();

}

//Capture user input

amount = sc.nextDouble();

//Call add() member function of Account with argument 'amount'

// obj1.add(amount);

obj1.getState().add(obj1, amount);

//User Prompt

System.out.println(" Would you like to enter another amount?(Y/N): ");

//Accept user input

choice = sc.next().charAt(0);

//Capitalize user input

choice = toUpperCase(choice);

}

//Exit message

System.out.println(" Thank-you, this program will now end.");

}

//Member Function add() with parameter double amount

public void add(double amount){

//Update balance

balance = amount + balance;

//If statement block; Used for State transition and print balance

if (balance > 0)

{

this.setState(new Sound());

System.out.println(" State: Sound Balance = " + balance);

}

else if (balance == 0)

{

this.setState(new Empty());

System.out.println(" State: Empty Balance = " + balance);

}

else

{

this.setState(new Arrear());

System.out.println("State: Arrears Balance = " + balance);

}

}

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

public AccountState getState() {

return state;

}

public void setState(AccountState state) {

this.state = state;

}

}

// Account State class

package BankAccount;

import BankAccount.Account;

public abstract class AccountState {

double balance;

public abstract void add(Account acc, double amount);

}

// sound class

package BankAccount; import BankAccount.Account;

public class Sound extends AccountState{

public void add(Account acc, double amount) {

balance+=amount;

acc.add(amount);

}

}

// empty class

package BankAccount;

import BankAccount.Account;

public class Empty extends AccountState{

public void add(Account acc, double amount) {

balance+=amount;

acc.add(amount);

}

}

// arrear class

package BankAccount;

import BankAccount.Account;

public class Arrear extends AccountState{

public void add(Account acc, double amount) {

balance+=amount;

acc.add(amount);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Which team solution is more likely to be pursued and why?

Answered: 1 week ago

Question

4. I can tell when team members dont mean what they say.

Answered: 1 week ago