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

Russell Zaki
Russell Zaki
3,247 Points

How can I solve this challenge?

I am experiencing difficulty solving this challenge.

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.parameter)
    public class NotEnoughFundsException extends Exception 
        creditor.getAvailableFunds ("DEDUCT")
    }
}
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

Mihai Craciun
Mihai Craciun
13,519 Points

There are many problems with your code

1st: your method is not included in the CreditorTest class. Be careful where you place parentheses. I've commented below where the mistakes are:

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());
    }

  }
//^ Here is where your class ends so you should add the method inside this

    @Test (expected = NotEnoughFundsException.parameter)
    public class NotEnoughFundsException extends Exception
//*****************************************************  ^^^you didn't open the parenthesis
        creditor.getAvailableFunds ("DEDUCT")
    }
  }
//^ this parenthesis is extra

2nd: Ok now about your Test method. The task ask you to write a happy path test for deduct method so it will expect a NotEnoughFundsException. So what you have to do is deduct from creditor more money that is already inside. For that you have to add some credit first with .addFunds(int) method and then try to deduct more than you've added. The result should look like this

@Test(expected = NotEnoughFundsException.class)
    public void notEnoughFunds() throws Exception{
     creditor.addFunds(25);
     creditor.deduct(50);
    }
Russell Zaki
Russell Zaki
3,247 Points

Thanks a lot for the explanation. It helped me.