Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Unit Testing in Java What to Test Happy Path Exception

praveen gudimetla
praveen gudimetla
464 Points

@Test(expected= error

challenge is

Take a look at the deduct method in the Creditor class. Pay attention to the NotEnoughFundsException that is thrown.

Can you please write a happy path test for us in the CreditorTest fixture using the expected element (parameter) for the @Test annotation? Thanks!

I wrote my new method as

package com.teamtreehouse.vending;

import org.junit.Before; import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

private Creditor creditor;

@Before
public void setUp() throws Exception {
    creditor = new Creditor();
}

@Test
public void addingFundsIncrementsAvailableFunds() throws Exception {
    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
    creditor.addFunds(10);

    int refund = creditor.refund();

    assertEquals(10, refund);
}

@Test
public void refundingResetsAvailableFundsToZero() throws Exception {
    creditor.addFunds(10);

    creditor.refund();

    assertEquals(0, creditor.getAvailableFunds());
}


 @Test(expected=NotEnoughFundsException.class)
public void addingFundsIncrementsAvailableFundsExceptionOne() throws Exception {
    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(60, creditor.getAvailableFunds());
}

}

i am getting below error

FAILURES

addingFundsIncrementsAvailableFundsExceptionOne: Unexpected exception, expected but was

how to fix this error. please advise

com/teamtreehouse/vending/CreditorTest.java
package com.teamtreehouse.vending;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

    private Creditor creditor;

    @Before
    public void setUp() throws Exception {
        creditor = new Creditor();
    }

    @Test
    public void addingFundsIncrementsAvailableFunds() throws Exception {
        creditor.addFunds(25);
        creditor.addFunds(25);

        assertEquals(50, creditor.getAvailableFunds());
    }

    @Test
    public void refundingReturnsAllAvailableFunds() throws Exception {
        creditor.addFunds(10);

        int refund = creditor.refund();

        assertEquals(10, refund);
    }

    @Test
    public void refundingResetsAvailableFundsToZero() throws Exception {
        creditor.addFunds(10);

        creditor.refund();

        assertEquals(0, creditor.getAvailableFunds());
    }


     @Test(expected=NotEnoughFundsException.class)
    public void addingFundsIncrementsAvailableFundsExceptionOne() throws Exception {
        creditor.addFunds(25);
        creditor.addFunds(25);

        assertEquals(60, creditor.getAvailableFunds());
    }

}
com/teamtreehouse/vending/Creditor.java
package com.teamtreehouse.vending;

public class Creditor {
    private int funds;

    public Creditor() {
        funds = 0;
    }

    public void addFunds(int money) {
        funds += money;
    }

    public void deduct(int money) throws NotEnoughFundsException {
        if (money > funds) {
            throw new NotEnoughFundsException();
        }
        funds -= money;
    }

    public int refund() {
        int refund = funds;
        funds = 0;
        return refund;
    }

    public int getAvailableFunds() {
        return funds;
    }

}
com/teamtreehouse/vending/NotEnoughFundsException.java
package com.teamtreehouse.vending;

public class NotEnoughFundsException extends Exception {
}

when i change like below

package com.teamtreehouse.vending;

import org.junit.Before; import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

private Creditor creditor;

@Before
public void setUp() throws Exception {
    creditor = new Creditor();
}

@Test
public void addingFundsIncrementsAvailableFunds() throws Exception {
    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
    creditor.addFunds(10);

    int refund = creditor.refund();

    assertEquals(10, refund);
}

@Test
public void refundingResetsAvailableFundsToZero() throws Exception {
    creditor.addFunds(10);

    creditor.refund();

    assertEquals(0, creditor.getAvailableFunds());
}


 @Test(expected=NotEnoughFundsException.class)
public void addingFundsIncrementsAvailableFundsExceptionOne() throws Exception {
    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

}

i got below error. i expected pass as i added 50 dollar funds and checking the same

FAILURES

addingFundsIncrementsAvailableFundsExceptionOne: Expected exception: com.teamtreehouse.vending.NotEnoughFundsException

what it mean by above exception and message?

5 Answers

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hi praveen,

you have to use the Creditor's deduct method and deduct more funds than are available for the exception to be thrown. Right now you are just adding funds twice.

Kind Regards, Florian

praveen gudimetla
praveen gudimetla
464 Points

@Test(expected=NotEnoughFundsException.class) public void deductMoreAmountForException() throws Exception { creditor.addFunds(10);

    assertEquals(creditor.deduct(15));
}

you mean i need to write new method as above at the end? But what should i assert. i am ading 15 dollar and deducting 15 so obviously this case should throw NotEnoughFundsException

please advise

package com.teamtreehouse.vending;

import org.junit.Before; import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

private Creditor creditor;

@Before
public void setUp() throws Exception {
    creditor = new Creditor();
}

@Test
public void addingFundsIncrementsAvailableFunds() throws Exception {
    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
    creditor.addFunds(10);

    int refund = creditor.refund();

    assertEquals(10, refund);
}

@Test
public void refundingResetsAvailableFundsToZero() throws Exception {
    creditor.addFunds(10);

    creditor.refund();

    assertEquals(0, creditor.getAvailableFunds());
}

@Test(expected=NotEnoughFundsException.class) public void deductMoreAmountForException() throws Exception { creditor.addFunds(10);

    assertEquals(creditor.deduct(15));
}

}

praveen gudimetla
praveen gudimetla
464 Points

i tried as below

@Test(expected=NotEnoughFundsException.class) public void deductMoreAmountForException() { creditor.addFunds(10); //assertEquals(0, creditor.deduct(20)); creditor.deduct(20);

}

below complete code

package com.teamtreehouse.vending;

import org.junit.Before; import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

private Creditor creditor;

@Before
public void setUp() throws Exception {
    creditor = new Creditor();
}

@Test
public void addingFundsIncrementsAvailableFunds() throws Exception {
    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
    creditor.addFunds(10);

    int refund = creditor.refund();

    assertEquals(10, refund);
}

@Test
public void refundingResetsAvailableFundsToZero() throws Exception {
    creditor.addFunds(10);

    creditor.refund();

    assertEquals(0, creditor.getAvailableFunds());
}

@Test(expected=NotEnoughFundsException.class) public void deductMoreAmountForException() { creditor.addFunds(10); //assertEquals(0, creditor.deduct(20)); creditor.deduct(20);

} }

i am getting below error

./com/teamtreehouse/vending/CreditorTest.java:50: error: unreported exception NotEnoughFundsException; must be caught or declared to be thrown creditor.deduct(20); ^ 1 error

how to resolve this. please advise