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

Tony Gemenie
Tony Gemenie
3,781 Points

Argument Exception Not Found

Hey guys!

I have an error when doing this exercise. Anybody know what may be going On?

@Test (expected = NotEnoughFundsException.class)
public void noteEnoughFundsToVend() {
  creditor.addFunds(50);
  creditor.deduct(75);
}

1 Answer

Mihai Craciun
Mihai Craciun
13,520 Points

You have to throw an exception in the method signature. You almost got it.

@Test (expected = NotEnoughFundsException.class)
public void noteEnoughFundsToVend() throws Exception{
  creditor.addFunds(50);
  creditor.deduct(75);
}
Tony Gemenie
Tony Gemenie
3,781 Points

Hey Mihai!

Good to hear from you! From my reading of the deduct() method if you don't have enough in the member variable money then the exception is thrown. Is this incorrect?

Mihai Craciun
Mihai Craciun
13,520 Points

Well yes but the method that uses the deduct method has to throw that Exception in order to handle it first. Or you can surround the method with a try/catch statement.