thinking

one platform thousands thinking

Mock void method

I have a method that returns void in a class that is a dependency of the class I want to test.

This class is huge and I'm only using this single method from it. I need to replace the implementation of this method for the test as I want it to do something different and I need to be able to access the parameters this method receives.

I cannot find a way of doing this in EasyMock. I think I know how to do it with Mockito by using doAnswer but I don't want to add another library unless absolutely necessary.


If I understand what you want to do correctly, you should be able to use andAnswer():

mockObject.someMethod(eq(param1), eq(param2);

expectLastCall().andAnswer(newIAnswer(){
    publicObject answer(){
        
//supply your mock implementation here

        SomeClass arg1 
=(SomeClass) getCurrentArguments()[0];

        AnotherClass arg2 
=(AnotherClass) getCurrentArguments()[1];

        arg1.doSomething(blah);

        
//return the value to be returned by the method (null for void)

        returnnull;

    }

});

posted on 2010-04-21 18:23 lau 阅读(533) 评论(0)  编辑  收藏 所属分类: Unit Test


只有注册用户登录后才能发表评论。


网站导航: