简体   繁体   English

在可分页控制器上使用WebTestClient

[英]Use WebTestClient on a Pageable Controller

I'm using the reactive web test alternative that comes with Spring Boot 2, and i'm trying to build a test for a controller like this: 我正在使用Spring Boot 2随附的反应式网络测试替代方法,并且我正在尝试为这样的控制器构建测试:

@RestController
@RequestMapping("/users")
public class UserController{

    @Autowired
    private UserService service;

    @GetMapping
    public Page<UserDTO> get(Pageable pageable){
        return service.get(pageable);
    }
}

The test looks like this: 测试如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UsersTest{

    @Autowired
    private WebTestClient webClient;

    @Test
    public void test(){
         List<UserDTO> userList = webClient.get()
            .uri("/users")
            .exchange()
            .expectStatus().isOk()
            .expectBodyList(UserDTO.class).hasSize(1)
            .returnResult()
            .getResponseBody();

        assertNotNull(userList.get(1).getId());
    }
}

The result in the request is in fact a list of 1 element, but is not properly, because the structure of the response is: 请求中的结果实际上是1个元素的列表,但由于响应的结构为:

{
  "content": [
    {
      "id": 1,
      "name": "John",
      "lastName": "Doe",
      "active": true
    }
  ],
  "pageable": {
    "sort": {
      "sorted": false,
      "unsorted": true
    },
    "offset": 0,
    "pageSize": 25,
    "pageNumber": 0,
    "unpaged": false,
    "paged": true
  },
  "totalPages": 1,
  "totalElements": 1,
  "last": true,
  "size": 25,
  "number": 0,
  "numberOfElements": 1,
  "first": true,
  "sort": {
    "sorted": false,
    "unsorted": true
  }
}

So when i get the id value of that first element I obtain a null value. 因此,当我获得第一个元素的id值时,我将获得空值。 And the test fails. 测试失败。

Is there a way (maybe a Exchange Filter Function) than makes the WebTestClient understand pageable responses? 有没有一种方法(也许是Exchange筛选器功能)可以使WebTestClient理解可分页的响应?

Thanks in advance for any help. 在此先感谢您的帮助。

I think WebTestClient can understand pageable responses. 我认为WebTestClient可以理解可分页的响应。 The structure of the response is json, and WebTestClient provides a way to interact with json. 响应的结构为json,而WebTestClient提供了一种与json交互的方式。

So if you want to test if the response contains 1 user that has an id. 因此,如果要测试响应是否包含1个具有ID的用户。 You can do: 你可以做:

  webClient.get()
           .uri("/users")
           .exchange()
           .expectBody()
           .jsonPath("$.numberOfElements").isEqualTo(1)
           .jsonPath("$.content[0].id").isNotEmpty();

I consider that Dirk Deyne solution is better than mine, but maybe for someone it could be useful. 我认为Dirk Deyne解决方案比我的要好,但是对于某些人来说,它可能是有用的。

I built these 3 classes using lombok : 我使用lombok构建了这3个类:

@Data
@NoArgsConstructor
public class CustomResponsePage<T> {
    List<T> content;
    CustomPageable pageable;
    Integer totalPages;
    Integer totalElements;
    Boolean last;
    Integer size;
    Integer number;
    Long numberOfElements;
    Boolean first;
    CustomSort sort;
}

@Data
@NoArgsConstructor
public class CustomPageable{
    CustomSort sort;
    Long offset;
    Integer pageSize;
    Integer pageNumber;
    Boolean unpaged;
    Boolean paged;
}

@Data
@NoArgsConstructor
public class CustomSort {
    Boolean sorted;
    Boolean unsorted;
}

An then, I modified the test like this: 然后,我像这样修改了测试:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UsersTest{

    @Autowired
    private WebTestClient webClient;

    @Test
    public void test(){
         PagedUserDTO pagedUserList = webClient.get()
            .uri("/users")
            .exchange()
            .expectStatus().isOk()
            .expectBody(UserDTO.class)
            .returnResult(PagedUserDTO.class)
            .getResponseBody();

        assertTrue(pagedUserList.getContent().size() == 0);
    }

    private static class PagedUserDTO extends CustomResponsePage<UserDTO> { }
}

Notice the PagedUserDTO class created at the end of the test. 注意测试结束时创建的PagedUserDTO类。

It's a still in progress structure because it doesn't consider when there are order columns in the response, but in my case I don't need them. 这是一个仍在进行中的结构,因为它不考虑响应中何时有订单列,但就我而言,我不需要它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM