Search code examples
javajukito

Jukito/Mockito Test with static method


I am trying to test a class (using Jukito and Mockito), which unfortunately extends another class, which has a static method call. Is it possible to somehow skip this call? I would rather not use PowerMockito.

public class A extends B {

    @Inject
    public A(final String s){
        super(s);
    }
}

public abstract class B {

    private String s;

    protected String m = C.get().createUniqueId(); //Exception is thrown here

    public B(String s){
        this.s = s;
    }
}

public class C {
    private static C c; //assume this is never null

    public static C get() {
        return c;
    }   

    public final native String createUniqueId() {}

}

@RunWith(JukitoRunner.class)
public class ATest {

    @Inject 
    A a;

    @Test
    public void onMethod1Test(){
    }
}

When running ATest, I get the following error:

Error injecting constructor, java.lang.UnsatisfiedLinkError: C

I assumed it's because of a static method, was I wrong?

Note that all the classes are just examples from my real classes and C class was not written by my and can not be changed (unfortunately). But idea behind my classes and these are the same, I just changed names and left only relevant parts.


Solution

  • Jukito claims:

    The combined power of JUnit, Guice and Mockito.

    But thing is: none of these products allows you to mock static methods.

    The only frameworks capable of that: PowerMock(ito) and JMockit.

    As you already explained: normally you would "bypass" this "deficiency" by simply writing testable code (that avoids static calls). But as you can't improve your design, you only these two choices: use PowerMock(ito) for testing this class - or not testing it.