jeudi 30 avril 2015

Mockito always return null when testing dropwizard resources

I am trying to test dropwizard resources and followed http://ift.tt/1zhs86A to do so.

However, I always get a null object from the mocked class/method.

The resource method

    @GET
    @Path("/id")
    @ApiOperation("Find property by id")
    @Produces(MediaType.APPLICATION_JSON)
    public Property findById(@QueryParam("id") int id) {

        return propertyDAO.findById(id);
    }

And the test class

public class PropertiesResourceTest {

    private static final PropertiesDAO dao = mock(PropertiesDAO.class);

    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule.builder()
            .addResource(new PropertiesResource(dao))
            .build();

    private final Property property = new Property(1);

    @Before
    public void setUp() {

        when(dao.findById(eq(1))).thenReturn(property);
        reset(dao);
    }

    @Test
    public void findById() {
        assertThat(resources.client().target("/properties/id?id=1").request().get(Property.class))
                .isEqualTo(property);
        verify(dao).findById(1);
    }

}

I tried to spin it in many ways, but the result is always the same:

expected:<Property | ID: 1 > but was:<null>

Do you have any leads on why mockito is always returning a null object ?

Aucun commentaire:

Enregistrer un commentaire