开发者

EasyMock : mocking the new operation possible?

Is it possible to do something like that with EasyMock:

class ClassName{
   void method(){
      TypeToMock a = new TypeToMock();
   }
}

and I want to mock TypeToMock without passing it as a value to cl开发者_如何学运维ass ClassName. Is it possible? If so how?


No I don't think this is possible.

If you want to mock out a dependency then you have to inject that dependency into the class under test, either through the constructor, through a setter method or directly into the method you want to test as an argument.

If your class is creating this using new and only doing it once then you should inject the dependency using the method that gives you best match of scope.

If your class is creating an object using new several times, then you could inject a factory which produces the objects instances instead. then you can provide a mock factory which produces mock instances.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜