我正在使用JUnit和Mockito编写使用Jersey的REST服务的测试用例。我得到一个null对象而不是Response类的模拟对象。被测Mocking使用Mockito的泛型类型
代码
Response response = builder
.put(Entity.entity(new ByteArrayInputStream(jsonObj.toString().getBytes()), MediaType.APPLICATION_JSON), Response.class);
测试用例:
private Invocation.Builder builder;
private Entity<ByteArrayInputStream> inputStream;
private Response response;
@Before
public void setUp() throws Exception {
builder = mock(Invocation.Builder.class);
inputStream = (Entity<ByteArrayInputStream>)mock(Entity.class);
response = mock(Response.class);
}
@Test
public void myTest() {
when(builder.put(inputStream, Response.class)).thenReturn(response);
}
所以这行代码给了我一个空的回应。有没有其他的方式来做到这一点。
谢谢。