开发者

java EasyMock ignore calls to an object from TestedClass' methods

开发者 https://www.devze.com 2023-02-18 04:26 出处:网络
I have a class where I have an object. I am tes开发者_开发百科ting a method that calls this object, but it the object has nothing to do with my test, so I\'d like to ignore it.

I have a class where I have an object. I am tes开发者_开发百科ting a method that calls this object, but it the object has nothing to do with my test, so I'd like to ignore it.

Class TestedClass {
 ObjectX obj;

  method() {
   /* some processing */
   obj.someMethod().otherMethod(lotofparameters);  /* i want to ignore this line in my test */ 
  }
}

So I want to test the method() without calling those methods on obj with parameters.

Thank u


Mock it using the createMock method. Then use the anyTimes method to basically ignore any calls to it.

expect(objMock.someMethod())
        .andReturn(42).times(3)

I threw in a return in case you needed it for something. Check out their documentation for more.

EDIT (to address the first comment)

Make someMethod return another mock. Then mock otherObj.otherMethod. As for the parameters, consider using the matchers listed in the documentation I linked to. You can even make your own matcher, so maybe that can help solve your issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消