Junit testing with Spring

PHOTO EMBED

Thu Dec 02 2021 11:46:38 GMT+0000 (Coordinated Universal Time)

Saved by @lilith #java

@EntendWith(SpringExtenstion.class)
//to focus on mvc components only
@WebMvcTest(ClassNameYoureGoingToTest.class)
class HelloControllerIntegrationTest{
  
  @Autowired 
  private MockMvc mvc;

  @Test
  void hello() throws Exception{
    RequestBuilder request = MockMvcRequestBuilders.get("/find/by/id");
    MvcResults result = mvc.perform(request).andReturn();
    //first param is expected value, second- actual value
    assertEquals("Hello, World", results.getResponse().getContentAsString());
  }

  @Test
  public void testHelloWithName throws Exception{
      mvc.perform(get("/find/by/id?id=123")).andExpect(content().string("Hello, Dan"));
  }

}

content_copyCOPY

https://www.youtube.com/watch?v=pNiRNRgi5Ws&ab_channel=DanVega