Sunday 15 April 2012

java - How do I ignore an exception thrown by a private void method using easymock? -



java - How do I ignore an exception thrown by a private void method using easymock? -

i have void method something. not care in unit test class. however, throwing exception when phone call unit-test class , causing test cases fail.

how can i, using easymock, mock object when void method gets called, ignore in unit test?

for example, let's object called myobject , has void method called dosomething(). easymock doesn't allow me utilize 'expect' because void method returns nothing. when phone call method in test case, throws exception (which want ignore).

i know can achieved maybe using mockito or other libs, i'd utilize easymock.

update

the method in question private void method gets called other processes. cannot phone call straight still want ignore exception that's thrown private void method.

i've tried using nice mock suggested below.

myobject obj = easymock.createnicemock(myobject.class); easymock.replay(obj);

and set obj in object contains private variable.

myobject2 obj2 = new myobject2(); obj2.setmyobject(obj);

and phone call method calls private void method internally.

obj2.methodthatcallsprivatevoidmethod();

and did not work.

without using easymock, mutual approach override problematic method in testable version of class.

assuming class

class myclass { ... public void dosomething() { ... } }

do in test:

public class mytestclass { @test public void testthis { myclass myobject = new testablemyclass(); ... // can invoke methods on myobject , // dosomething() method harmless. } class testablemyclass extends myclass { @override public void dosomething { // nothing. no exception. } } }

note easymock may provide similar way this, not familiar easymock. know not problem mockito.

java exception mocking void easymock

No comments:

Post a Comment