Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RunWith(SpringRunner.class)
- @SpringBootTest
- @AutoConfigureMockMvc
- public abstract class AbstractCrudControllerTest {
- @Autowired
- protected MockMvc mockMvc;
- // Define abstract methods for creating, updating, and deleting entities
- protected abstract Object createEntity();
- protected abstract Object updateEntity(Object entity);
- protected abstract String getEndpoint();
- protected abstract Long getEntityId(Object entity);
- @Test
- public void testCreateEntity() throws Exception {
- Object entity = createEntity();
- mockMvc.perform(post(getEndpoint())
- .contentType(MediaType.APPLICATION_JSON)
- .content(JsonUtils.toJson(entity)))
- .andExpect(status().isCreated())
- .andExpect(jsonPath("$.id").isNotEmpty());
- }
- @Test
- public void testGetEntity() throws Exception {
- Object entity = createEntity();
- mockMvc.perform(get(getEndpoint() + "/" + getEntityId(entity)))
- .andExpect(status().isOk());
- }
- @Test
- public void testUpdateEntity() throws Exception {
- Object entity = createEntity();
- Object updatedEntity = updateEntity(entity);
- mockMvc.perform(put(getEndpoint() + "/" + getEntityId(entity))
- .contentType(MediaType.APPLICATION_JSON)
- .content(JsonUtils.toJson(updatedEntity)))
- .andExpect(status().isOk());
- }
- @Test
- public void testDeleteEntity() throws Exception {
- Object entity = createEntity();
- mockMvc.perform(delete(getEndpoint() + "/" + getEntityId(entity)))
- .andExpect(status().isNoContent());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement