我正在使用TestRestTemplate来测试@RequestParam值如何执行

时间:2017-08-25 15:32:37

标签: testing spring-boot

//如何将@RequestParam值发送到url

enter code here@ApiRestController

公共类CityController扩展了BaseController {

    @GetMapping("/cities")

    public ResponseEntity<CitiesResponse> getAll(
            @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "100") int pageSize,
            @RequestParam(value = "sortBy", defaultValue = "id", required = false) String sortBy,
            @RequestParam(value = "sortDirection", defaultValue = "asc", required = false) String sortDirection,
            @RequestParam(value = "search", required = false) String search) {
        return new ResponseEntity(cityService.getAll(pageNumber, pageSize, sortBy, sortDirection, search), HttpStatus.OK);
    }
}

2 个答案:

答案 0 :(得分:4)

为了轻松操作URL / path / params /等,您可以使用Spring的UriComponentsBuilder类。手动连接字符串比较干净,它会为您处理URL编码:

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
        .queryParam("pageNumber", 1)
        .queryParam("pageSize", 10)
        .queryParam("sortBy", "id")
        .queryParam("sortDirection", "desc")
        .queryParam("search", "hello search");

HttpEntity<?> entity = new HttpEntity<>(headers); //Update this as per your code

HttpEntity<String> response = restTemplate.exchange(
        builder.build().encode().toUri(), 
        HttpMethod.GET, 
        entity, 
        String.class);

答案 1 :(得分:1)

在春季启动中有不同的测试方法。检查以下示例:

第一个选项: 它更像是集成测试。在这种情况下,端口将是默认的8080

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {

    private TestRestTemplate restTemplate = new TestRestTemplate();

    @Test
    public void contextLoads() {
        String url = "http://localhost:8080";
        URI uri = UriComponentsBuilder.fromHttpUrl(url).path("/books")
                .queryParam("order", "asc").build().toUri();
        this.restTemplate.getForEntity(uri, Void.class);
    }

}

第二个选项: 与第一个选项非常相似,但这次它将在随机端口中运行,可以通过@LocalServerPort

捕获
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoApplicationTests {

    @LocalServerPort
    private int port;

    private TestRestTemplate restTemplate = new TestRestTemplate();

    @Test
    public void contextLoads() {
        String url = "http://localhost:" + this.port;
        URI uri = UriComponentsBuilder.fromHttpUrl(url).path("/books")
                .queryParam("order", "asc").build().toUri();
        this.restTemplate.getForEntity(uri, Void.class);
    }

}
  

UriComponentsBuilder已被用于以非常友好的方式构建uri。

第三个选项: 此选项不涉及TestRestTemplate,但仅涉及RestController。控制器内的任何依赖项都应在测试中标记为@MockBean

@RunWith(SpringRunner.class)
@WebMvcTest(BookRestController.class)
public class DemoApplicationTests {

    @Autowired
    private MockMvc mvc;

    @Test
    public void contextLoads() throws Exception {

        this.mvc.perform(MockMvcRequestBuilders.get("/books")
                .param("order", "asc"))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }

}