1
我正在嘗試測試dropwizard資源並遵循http://www.dropwizard.io/manual/testing.html這樣做。Mockito在測試dropwizard資源時總是返回null
但是,我總是從模擬類/方法中得到一個空對象。
資源方法
@GET
@Path("/id")
@ApiOperation("Find property by id")
@Produces(MediaType.APPLICATION_JSON)
public Property findById(@QueryParam("id") int id) {
return propertyDAO.findById(id);
}
,並且測試類
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);
}
}
我試着旋轉它在許多方面,但結果總是相同的:
expected:<Property | ID: 1 > but was:<null>
你h對於爲什麼mockito總是返回一個空對象的任何線索?
嗯,我覺得這封信的文件是愚蠢的...無論如何,它的工作原理。謝謝 ! –