Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Unit Testing in Java What to Test Happy Path Exception

Enyang Mercy
PLUS
Enyang Mercy
Courses Plus Student 2,339 Points

Unit Test /Exception Creditor class challenge

Tried to use the @Test annotation but don't understand the challenge properly I keep having errors trying to compile. I need help with my work. Thank you

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 = NotEnoughFunds.class)
  public void moneyMoreThanFundsIsNotAllowed() throws Exception {
}
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 {
}

2 Answers

The class is called NotEnoughFundsException not NotEnoughFunds.

In your test you need to call the public method deduct(int) from the Creditor class. Since you do not have any funds to begin with, any value greater than 0 will have the exception being thrown. You could also add some funds and then deduct a value greater than the funds previously added. Both ways will meet the happy path.