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,
nullpointerexceptionusing uninitializeddependency.if add
@mockedannotationd dependencyline, methods indmocked,d.dosomething()doesn't it's supposed do.if keep
@mockedannotation , add emptynonstrictexpectationsblock @ 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
nonstrictexpectationsblock , remove@mockedannotation, againnullpointerexceptionusing 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