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.
精彩评论