braydoncoyer
10/20/2019 - 8:39 PM

Integration Tests

This is a JUnit integration test that starts from the controller, hits the service and then the client CRUD.

@WebMvcTest(AttachmentController.class)
@ExtendWith(SpringExtension.class)
class AttachmentControllerIT {

    private MockMvc mockMvc;

    @MockBean
    private AttachmentController attachmentController;

    @BeforeEach
    public void beforeEachTest() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(attachmentController).build();
    }

    @Test
    void findByIdITShouldReturnOk() throws Exception {

        ObjectMapper objectMapper = new ObjectMapper();
        Attachment attachment = new Attachment();
        attachment.set_id(22L);
        attachment.setUserId(10L);
        attachment.setImgSrc("test");

        mockMvc.perform(get("/attachements/22")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(objectMapper.writeValueAsString(attachment)))
                .andDo(print())
                .andExpect(status().isOk());


        then(attachmentController).should().findById(22L);
    }


}