2017-08-25 57 views
1

//如何發送@RequestParam值網址我使用TestRestTemplate與@RequestParam值來測試如何執行

enter code [email protected] 

公共類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); 
    } 
} 

回答

3

要輕鬆操作URLs/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

有不同的方法在春季開機測試。查看下面的示例:

第一個選項: 它更像是一個集成測試。在這種情況下,端口將成爲默認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()); 
    } 

} 
+0

但在這個單元測試我送多@RequestParam與值(定義)並取回響應PAGENUMBER,pageSize的,sortBy,sortDirection ,搜索值 –

+0

如何發送不帶請求的@RequestParam值(pojo類)? –

相關問題