【发布时间】:2015-07-10 15:07:51
【问题描述】:
我正在尝试测试 dropwizard 资源并按照 http://www.dropwizard.io/manual/testing.html 这样做。
但是,我总是从模拟的类/方法中得到一个空对象。
资源方法
@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>
你对为什么 mockito 总是返回一个空对象有任何线索吗?
【问题讨论】:
标签: unit-testing junit mockito dropwizard