我正在使用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);
}
所以這行代碼給了我一個空的迴應。有沒有其他的方式來做到這一點。
謝謝。