Question
public class Money{ private int cAmount; private String cCurrency; // constructor for creating a money object public Money(int amount, String currency) { cAmount = amount;
public class Money{
private int cAmount; private String cCurrency; // constructor for creating a money object public Money(int amount, String currency) { cAmount = amount; cCurrency = currency; }
// set money public int getAmount() { return cAmount; }
// get money public String getCurrency() { return cCurrency; }
public Money add(Money m) throws Exception { if (m.getAmount()
@Override public boolean equals(Object anObject) { if (anObject instanceof Money) { Money passedMoney = (Money) anObject; if (this.cAmount == passedMoney.getAmount() && this.cCurrency.equals(passedMoney.getCurrency())) return true; } return false; } }
####
JUNit 4 test
import org.junit.Test;
import static org.junit.Assert.*; public class MoneyTest {
//Testing that two Money objects are successfully added together @Test public void simpleAdd() throws Exception { Money m12CAD= new Money(12, "CAD"); Money m14CAD= new Money(14, "CAD"); Money known= new Money(26, "CAD"); Money observed= m12CAD.add(m14CAD); assertTrue(known.equals(observed)); }
//testing that exception is thrown correctly @Test (expected = Exception.class) public void testNegativeMoneyValue () throws Exception{ Money m12CAD= new Money(12, "CAD"); Money m14CAD= new Money(14, "CAD"); Money observed= m12CAD.add(m14CAD); } }
##################
Resolve the issue with both test cases so that they complete successfully. Hint:
-
The SimpleAdd test fails is not being done correctly in Money.java
-
The TestNegativeMoneyValue test fails because of a problem with the test itself (i.e. it's not testing the correct thing)
##########################################
##add the following code to Moeny test and resolve the error
//Test is unable to complete successfully because exception is thrown
@Test
public void simpleAddFailure() throws Exception {
Money m12CAD= new Money(12, "CAD");
Money m14CAD= new Money(-14, "CAD");
Money known= new Money(26, "CAD");
Money observed= m12CAD.add(m14CAD); assertTrue(known.equals(observed));
}
Java Project Project... F4 New Go Into Open in New Window Open Type Hierarchy Show in o Copy Copy Qualified Name Paste x Delete *W XC Flat XV Plat Package e Class Interface Enum Annotation Source Folder Java Working Set Folder File Untitled Text File Task JUnit Test Case Example... Remove from Context VOI Build Path Source VAS Refactor TXT Convert to Xtend La Import... Export... Refresh Assign Working Sets... Other... EN New JUnit Test Case JUnit Test Case The use of the default package is discouraged. !!! New JUnit Jupiter test New JUnit 3 tist New JUnit 4 test Source folder: Labowo- Browse... Package: (default) Browse... Name: MoneyTest Superclass: java.lang.Object Browse
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started