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

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,519 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,519 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.