Search code examples
guicejmockit

GenericSignatureFormatError when mixing Guice and JMockit


Guice and JMockit do not seem to get along ...

The following test throws GenericSignatureFormatError. Why?

@RunWith(JMockit.class)
public class GuiceAndJMockitTest {

    public interface Foo {
    }

    @Test
    public void guiceAndJMockitGetAlong(@Mocked final Foo foo) {
        Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(Foo.class).toInstance(foo);
            }
        });
    }

}

Thanks

JMockit 1.13

Guice 3.0


Solution

  • This appears to be due to some invalid "generic info" inside the class generated by JMockit that implements the given interface. So, apparently a bug in JMockit; I will look into that.

    However, if you want to mock objects that implement an interface and get injected by Guice, I would recommend to use @Capturing instead of @Mocked. That way, any class implementing the declared mocked type will get mocked as well. And there will be no need to create a test-specific Guice injector; simply use the same Guice module as used for production.