[Spring Boot] Spring Boot Test

build.gradle

dependencies {
  // Spring Boot MVC
  implementation 'org.springframework.boot:spring-boot-starter-web'
  // Spring Boot Test
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Path Parameter

1. Controller Class

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
public class HttpRequestController {
  @GetMapping("/{greeting}")
  public String greeting(@PathVariable String greeting) {
    return "Hello, " + greeting;
  }
}

2. Test Class (With TestRestTemplate)

  • TestRestTemplate -> package org.springframework.boot.test.web.client;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {
  @LocalServerPort
  private int port;

  @Autowired
  private TestRestTemplate restTemplate;

  @Autowired
  private HttpRequestController httpRequestController;

  @Test
  public void getRequestTest() throws Exception {
      assertThat(httpRequestController).isNotNull();

      assertThat(this.restTemplate.getForObject(
        "http://localhost:" + port + "/",
        String.class))
        .contains("Hello, World");
  }
}

3. Test Class (With MockMvc)

  • MockMvc -> package org.springframework.test.web.servlet;
@SpringBootTest
@AutoConfigureMockMvc
public class HttpRequestTestMockMvc {
  @Autowired
  private MockMvc mockMvc;
  
  @Autowired
  private HttpRequestController httpRequestController;

  @Test
  public void getRequestTestMockMvc() throws Exception {
      assertThat(httpRequestController).isNotNull();

      this.mockMvc.perform(get("/"))
              .andDo(print())
              .andExpect(status().isOk())
              .andExpect(content().string(containsString("Hello, World")));
  }
}

Query Parameter

1. Controller Class

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
public class HttpRequestController {
  @GetMapping("/")
  public String greeting(@RequestParam String greeting) {
      return "Hello, " + greeting;
  }
}

2. Test Class (With TestRestTemplate)

  • TestRestTemplate -> package org.springframework.boot.test.web.client;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {
  @LocalServerPort
  private int port;

  @Autowired
  private TestRestTemplate restTemplate;

  @Autowired
  private HttpRequestController httpRequestController;

  @Test
  public void getRequestTest() throws Exception {
      assertThat(httpRequestController).isNotNull();

      assertThat(this.restTemplate.getForObject(
        "http://localhost:" + port + "/?" + "greeting=World",
        String.class))
        .contains("Hello, World");
  }
}

3. Test Class (With MockMvc)

  • MockMvc -> package org.springframework.test.web.servlet;
@SpringBootTest
@AutoConfigureMockMvc
public class HttpRequestTestMockMvc {
  @Autowired
  private MockMvc mockMvc;
  
  @Autowired
  private HttpRequestController httpRequestController;

  @Test
  public void getRequestTestMockMvc() throws Exception {
      assertThat(httpRequestController).isNotNull();

      this.mockMvc.perform(get("/")
        .param("greeting", "World"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().string(containsString("Hello, World")));
  }
}

Comments