java - How to test a code with Mockito that captures an Exception and returns false -
i know there several ways use mockito in order test whether exception thrown. but, problem method trying test not raise exception. rather, method has try-catch clause , within catch clause returns false after capturing exception. how test method?
boolean method() throws dataexception { try { a.do(); return true; } catch(nullpointerexception e) { /* come down here when null */ return false; } }
to give little more twist, method() forced throw dataexception since do() method throws dataexception.
so, have 2 issues:
- i must throw dataexception within test code
- i must test method() see whether captures nullpointerexception , returns false.
editing. yes, a mocked object.
you can mock object a
, throw nullpointerexception
@test public void itshouldreturnfalse when(a.do()).thenthrow(new nullpointerexception()); assertfalse(yourclass.method()); } @test(expected = dataexception.class) public void itshouldthrowexception() { when(a.do()).thenthrow(new dataexception()); yourclass.method(); }
Comments
Post a Comment