testing our code

About me

Alejandro Vidal Rodriguez
Director of Engineering @TribalScale

Agenda

  • Why we should test

  • Testing piramid

  • Slices

  • Mocking

  • Unit Testing

  • Integration testing

  • End to end testing

  • Coverage

  • How to pass your tests

Why we should test?

Bad Reasons

We want 100% coverage

Managements wants them

Somebody told me

They are cool

Increase a dashboard

Good Reasons

Sleep well

Go on holidays

Release with calm

Fearless refactoring

Avoid bugs

Release faster

Testing piramid

    https://martinfowler.com/articles/practical-test-pyramid.html

Testing piramid

Slices

Slices - Junit test

Unit test do not need any spring dependency

Slices - Junit test

@RunWith(MockitoJUnitRunner.class)
public class B2BClientBayPlanTest {

    @Mock
    MessageUtil messageUtil;
    @Mock
    SecurityUtility securityUtility;
    @Mock
    CustomUserDetails customUserDetails;


    @Test
    public void createHeader() {
        Mockito.when(securityUtility.getUserInfo()).thenReturn(customUserDetails);
        Mockito.when(customUserDetails.getUsername()).thenReturn("Alex");

        B2BClientBayPlan b2BClientBayPlan = new B2BClientBayPlan(messageUtil, securityUtility);

        B2bHeader b2bHeader = b2BClientBayPlan.createHeader("myAction");

        assertEquals("myAction", b2bHeader.getMessageHeader().getAction());
    }

    @Test
    public void sendToB2B() {
    }
}

Slices - Spring tests

Slices - Spring tests

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = VoyageEnquiryController.class)
@TestPropertySource(properties = "eureka.client.enabled=false")
public class VoyageEnquiryTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    VoyageEnquiryService voyageEnquiryService;

    @MockBean
    ClientRegNewService clientRegNewService;

    @Test
    @WithMockUser
    public void searchVoyageEnquiry() throws Exception {
        List<VoyageEnquirySearchResultSO> resultList = new LinkedList<>();
        Mockito.when(voyageEnquiryService.searchVoyageEnquiry(any(VoyageEnquirySearchSO.class))).thenReturn(resultList);

        MvcResult result = this.mockMvc
                .perform(post("/searchVoyageEnquiry")
                        .content(VOYAGE_SEARCH)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content(VOYAGE_SEARCH_ID))
                .andExpect(status().isOk())
                .andReturn();

        assertEquals("{\"list\":[]}", result.getResponse().getContentAsString());
        Mockito.verify(voyageEnquiryService, Mockito.atLeastOnce()).searchVoyageEnquiry(any(VoyageEnquirySearchSO.class));
    }

Slices - Integration test

Slices - Security

Slices - Performance

Slices - UI

Mocking

Unit testing

Unit testing

@RunWith(MockitoJUnitRunner.class)
public class B2BClientBayPlanTest {

    @Mock
    MessageUtil messageUtil;
    @Mock
    SecurityUtility securityUtility;
    @Mock
    CustomUserDetails customUserDetails;


    @Test
    public void createHeader() {
        Mockito.when(securityUtility.getUserInfo()).thenReturn(customUserDetails);
        Mockito.when(customUserDetails.getUsername()).thenReturn("Alex");

        B2BClientBayPlan b2BClientBayPlan = new B2BClientBayPlan(messageUtil, securityUtility);

        B2bHeader b2bHeader = b2BClientBayPlan.createHeader("myAction");

        assertEquals("myAction", b2bHeader.getMessageHeader().getAction());
    }

    @Test
    public void sendToB2B() {
    }
}

Integration testing

Integration testing

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = VoyageEnquiryController.class)
@TestPropertySource(properties = "eureka.client.enabled=false")
public class VoyageEnquiryTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    VoyageEnquiryService voyageEnquiryService;

    @MockBean
    ClientRegNewService clientRegNewService;

    @Test
    @WithMockUser
    public void searchVoyageEnquiry() throws Exception {
        List<VoyageEnquirySearchResultSO> resultList = new LinkedList<>();
        Mockito.when(voyageEnquiryService.searchVoyageEnquiry(any(VoyageEnquirySearchSO.class))).thenReturn(resultList);

        MvcResult result = this.mockMvc
                .perform(post("/searchVoyageEnquiry")
                        .content(VOYAGE_SEARCH)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content(VOYAGE_SEARCH_ID))
                .andExpect(status().isOk())
                .andReturn();

        assertEquals("{\"list\":[]}", result.getResponse().getContentAsString());
        Mockito.verify(voyageEnquiryService, Mockito.atLeastOnce()).searchVoyageEnquiry(any(VoyageEnquirySearchSO.class));
    }

Test coverage

Line coverage

Branch coverage

Fast coverage

how to pass your tests

Lets do test NOW!

Further reads

Questions?

contact

https://www.linkedin.com/in/alejandrovidalrodriguez

https://medium.com/@goofyahead​

https://github.com/goofyahead

Testing & Mocking

By Alejandro Vidal Rodriguez

Testing & Mocking

Why and how we should do unit testing and mocking.

  • 19