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:

  1. i must throw dataexception within test code
  2. 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

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -