mardi 28 juillet 2015

JMockit and Jersey JAX-RS Testing

I have a Jersey REST webservice which I want to build a unit test, I followed the steps provided by Jersey Test documentation, but in my webservice I have references to additional classes which handles the business logic of the flow, I am trying to use JMockit to mock the business service class that is referenced by the service but I get errors:

public class ProdServiceTest extends JerseyTest {

    @Injectable
    private ProdServiceBusinessRemote prodServiceBusinessRemote;

    @Before 
    public void setUp() throws Exception {
        initMocks(); 
    }

    private void initMocks() throws Exception {
        new NonStrictExpectations() {{
            prodServiceBusinessRemote.query();
            result = null;          
        }};

    }

    @Override
    protected Application configure() {
        return new ResourceConfig(ProdService.class);
    }

    @Test
    public void test() throws Exception {
        List list = target("prod").request().get(List.class);
        org.junit.Assert.assertTrue(list == null);
    }
}

If I run without the @RunWith(JMockit.class)

java.lang.IllegalStateException: JMockit wasn't properly initialized; please ensure that jmockit precedes junit in the runtime classpath, or use @RunWith(JMockit.class)

If I run with @RunWith(JMockit.class) I get an NullPointerException inside JerseyTest internal function:

public final WebTarget target() {
    return client().target(getTestContainer().getBaseUri());
}

where client() is null;

Is there a way to use Jmockit and JerseyTest together ? How does Jersey recommend in the case of Services which references external classes? How to mock these ones?

Aucun commentaire:

Enregistrer un commentaire