java - How to partially mock a dependency abstract object in JMockit -
i have abstract class d
dependency of tested class t
.
the test class:
public class t_test { @tested t tested; d dependency; public void test() { dependency.dosomething(); tested.testedmethod(dependency); } }
i want dependency.dosomething()
run real code of method, abstract methods mocked.
if run test is,
nullpointerexception
using uninitializeddependency
.if add
@mocked
annotationd dependency
line, methods ind
mocked,d.dosomething()
doesn't it's supposed do.if keep
@mocked
annotation , add emptynonstrictexpectations
block @ beginning of test method, in order have partial mock, either this:new nonstrictexpectations(d.class) {};
or this:
new nonstrictexpectations(d) {};
i
java.lang.illegalargumentexception: mocked: class d
.if keep
nonstrictexpectations
block , remove@mocked
annotation, againnullpointerexception
using uninitializeddependency
.
so how can partially mock dependency abstract class?
using @capturing
annotation on dependency achieves this. no need add empty expectations block; abstract methods mocked.
public class t_test { @tested t tested; @capturing d dependency; public void test() { dependency.dosomething(); tested.testedmethod(dependency); } }
Comments
Post a Comment