Question
These are my two classes of java (ECLIPSE) and they are part of the same project. I'm having an assertion failure in junit . As
These are my two classes of java (ECLIPSE) and they are part of the same project. I'm having an assertion failure in junit . As its a failure it means one of my assumption is wrong here... I cant figure out which one... I'm attaching the failure screenshot.
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; } }
second code:
import org.junit.Test; import static org.junit.Assert.*;
public class MoneyTest {
//Testing that two Money objects are successfully added together @Test (expected = Exception.class) 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 known= new Money(26, "CAD"); Money observed= m12CAD.add(m14CAD); assertTrue(known.equals(observed)); } @Test (expected = Exception.class) 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)); } }
Finished arter 0.037 seconds Runs: 3/3 * Errors: 1 Failures: 0 E MoneyTest (Runner: JUnit 4] (0.0145) simpleAdd(0.011 s) testNegativeMoneyValue (0.000 s) simpleAddFailure (0.002 s) = Failure Trace J! java.lang.Exception: Unexpected exception, expectedStep 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