java - How to test an exception scenario with Hamcrest -


i have method named execute() , returns boolean. under normal situations, returns true, if there exception, (say, dataaccessexception) capture exception , return false.

i trying figure out how test scenario in exception raised , "false" returned, using hamcrest.

so, here is:

public boolean execute() {     try {     ....... return true;}     catch (dataaccessexception de) { ....... return false;} } 

as suppressing dataaccessexception inside execute() method, not able test exception, rather can assert result of method call shown below:

@test public void testexecute() {    //mock code throw dataaccessexception     mokito.dothrow(new dataaccessexception()).when(mockobj).methodname(somemethod);      //now call execute method     boolean actual = obj.execute();     assertthat(actual, false); } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -