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.

  1. if run test is, nullpointerexception using uninitialized dependency.

  2. if add @mocked annotation d dependency line, methods in d mocked, d.dosomething() doesn't it's supposed do.

  3. if keep @mocked annotation , add empty nonstrictexpectations 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.

  4. if keep nonstrictexpectations block , remove @mocked annotation, again nullpointerexception using uninitialized dependency.

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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -