Java assertEquals 中方括号的事件驱动开发问题

Java Event Driven Development problem with square bracket in assertEquals

我做经典的事件驱动开发测试。

//给定

//当

// then
then(response.getStatus()).isEqualTo(HttpStatus.OK.value());

then(response.getContentAsString()).isEqualTo(
      jsonRequestProduct.write(List.of(newProduct)).getJson());

我会从第二行代码得到响应。

org.opentest4j.AssertionFailedError: Expecting: ... but was not.

Expected :"[{"id":1,"description":"iPhone 33","price":599.99}]"
Actual   :"{"id":1,"description":"iPhone 33","price":599.99}"

问题:如何去掉“[]”才能通过测试?

感谢您的建议。

更新:

Required type: List<xxx.demo.model.Product>

Provided: Product

这是我的控制器的样子。

    @PutMapping("/update")
    public ResponseEntity<Product> updateProduct(@RequestBody Product product) {
         return ResponseEntity.ok(productServiceImpl.updateProduct(product));
    }

删除 List.of 调用。您的断言告诉您它需要一个 list/array 但只有一个对象。

如果你这样做,测试就会通过。

@PutMapping("/update")
public Product updateProduct(@RequestBody Product product) {
     return productServiceImpl.updateProduct(product);
}