/** * Helper method to build a test instance with ID and value * * @param value * @return * @throws IllegalAccessException * @throws NoSuchFieldException */ private TestModel buildTestInstanceWithIdAndValue(int id, String value) throws Exception { TestModel tm = buildTestInstanceWithValue(value); TestUtil.setIdOnPersistentObject(tm, id); return tm; }
/** * Tests whether the REST findById interface will return an expected entity and a HTTP Status Code * 200 (OK). * * @throws Exception */ @Test public void findById_shouldReturn_EntityAndOK() throws Exception { int id = 42; String value = "find value"; TestModel testInstance = buildTestInstanceWithValue(value); TestUtil.setIdOnPersistentObject(testInstance, id); when(serviceMock.findById(id)).thenReturn(testInstance); // Test GET method with ID mockMvc .perform(get("/tests/" + id)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.id", is(id))) .andExpect(jsonPath("$.testValue", is(value))); verify(serviceMock, times(1)).findById(id); verifyNoMoreInteractions(serviceMock); }