Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package com.techelevator; import static org.junit.Assert. * ; import org.junit.Before; import org.junit.Test; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; import java.lang.reflect. * ; @FixMethodOrder ( MethodSorters . NAME _

package com.techelevator;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import java.lang.reflect.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CheckingAccountTest {
private final String PACKAGE_NAME = "com.techelevator";
private final String BANK_ACCOUNT_CLASS_NAME = "BankAccount";
private final String CHECKING_ACCOUNT_CLASS_NAME = "CheckingAccount";
private static Class checkingAccountClass;
@Before
public void Initialize(){
try {
checkingAccountClass = Class.forName(PACKAGE_NAME +"."+ CHECKING_ACCOUNT_CLASS_NAME);
} catch (ClassNotFoundException e){
fail(e.getMessage()+" class could not be found. Have you declared it yet? Make sure the class name is '"+ CHECKING_ACCOUNT_CLASS_NAME +"' and the package name is '"+ PACKAGE_NAME +"'.");
}
}
@Test
public void test01_ClassWellFormed(){
String wellFormedCheck = classWellFormedCheck();
if (!wellFormedCheck.isEmpty()){
fail(wellFormedCheck);
}
}
@Test
public void test02_HappyPath_Tests() throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
String wellFormedCheck = classWellFormedCheck();
if (!wellFormedCheck.isEmpty()){
fail(CHECKING_ACCOUNT_CLASS_NAME +" is not well formed. The test01_ClassWellFormed tests must pass first.\r
\t"+ wellFormedCheck);
}
// Assert constructors set fields
String testAccountHolderName = "Tester Testerson";
String testAccountNumber ="CHK:1234";
int testBalance =100;
// Two arg constructor
Constructor twoArgConstructor = SafeReflection.getConstructor(checkingAccountClass, String.class, String.class);
Object twoArgInstance = twoArgConstructor.newInstance(testAccountHolderName, testAccountNumber);
Method twoArgGetAccountHolderName = twoArgInstance.getClass().getMethod("getAccountHolderName");
assertEquals(CHECKING_ACCOUNT_CLASS_NAME +" constructor "+ CHECKING_ACCOUNT_CLASS_NAME +"(String, String) does not correctly set the field accountHolderName.",
testAccountHolderName, twoArgGetAccountHolderName.invoke(twoArgInstance));
Method twoArgGetAccountNumber = twoArgInstance.getClass().getMethod("getAccountNumber");
assertEquals(CHECKING_ACCOUNT_CLASS_NAME +" constructor "+ CHECKING_ACCOUNT_CLASS_NAME +"(String, String) does not correctly set the field accountNumber.",
testAccountNumber, twoArgGetAccountNumber.invoke(twoArgInstance));
// Three arg constructor
Constructor threeArgConstructor = SafeReflection.getConstructor(checkingAccountClass, String.class, String.class, int.class);
Object threeArgInstance = threeArgConstructor.newInstance(testAccountHolderName, testAccountNumber, testBalance);
Method threeArgGetAccountHolderName = threeArgInstance.getClass().getMethod("getAccountHolderName");
assertEquals(CHECKING_ACCOUNT_CLASS_NAME +" constructor "+ CHECKING_ACCOUNT_CLASS_NAME +"(String, String, int) does not correctly set the field accountHolderName.",
testAccountHolderName, threeArgGetAccountHolderName.invoke(threeArgInstance));
Method threeArgGetAccountNumber = threeArgInstance.getClass().getMethod("getAccountNumber");
assertEquals(CHECKING_ACCOUNT_CLASS_NAME +" constructor "+ CHECKING_ACCOUNT_CLASS_NAME +"(String, String, int) does not correctly set the field accountNumber.",
testAccountNumber, threeArgGetAccountNumber.invoke(threeArgInstance));
Method threeArgGetBalance = threeArgInstance.getClass().getMethod("getBalance");
assertEquals(CHECKING_ACCOUNT_CLASS_NAME +" constructor "+ CHECKING_ACCOUNT_CLASS_NAME +"(String, String, int) does not correctly set the field balance.",
testBalance, threeArgGetBalance.invoke(threeArgInstance));
// Assert withdraw decreases balance
Method withdraw = threeArgInstance.getClass().getMethod("withdraw", int.class);
int withdrawParamValue =22;
int withdrawExpectedReturnValue = testBalance - withdrawParamValue;
Object withdrawActualReturnValue = withdraw.invoke(threeArgInstance, withdrawParamValue);
assertEquals(CHECKING_ACCOUNT_CLASS_NAME +" withdraw method fails to decrease balance by correct amount. Starting balance: "+ testBalance +", withdraw: "+ withdrawParamValue +", new balance should be "+ withdrawExpectedReturnValue +".",
withdrawExpectedReturnValue, withdrawActualReturnValue);
}
@Test
public void test03_EdgeCase_Tests() throws IllegalAccessException, InvocationTargetException, InstantiationException {

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

Step: 3

blur-text-image

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

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

Explain the steps involved in training programmes.

Answered: 1 week ago

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

What has been the evolution of HRM?

Answered: 1 week ago

Question

What would you do?

Answered: 1 week ago