Spring WebTestClient响应对象与预期的不同。

huangapple go评论113阅读模式
英文:

Spring WebTestClient response object is different from expected

问题

以下是您要翻译的内容:

I have a controller unit test using WebTestClient and it's failing on the isEqualTo() test. I can't find what's wrong as I'm using the same object to set what the service call will return and the parameter for isEqualTo()

当我运行测试时,我得到了类似以下的结果:

Expected :com.model.Response@ab41b46
Actual   :com.model.Response@c1dcdb9

模型:这只是其中一部分,但其他对象只是字符串。

public class Response implements Serializable {
    private static final long serialVersionUID = 1L; // 随机长整数
    private String id;
    private Long amount;
    private LocalDateTime dateTime;
    private String status;

    // setter 和 getter 方法
}

控制器:

@PostMapping
public Mono<Response> getResponse() {
     return myService.getResponse();
}

测试:

@ExtendWith(MockitoExtension.class)
@WebFluxTest(controllers = MyController.class)
public class MyControllerTest
{
    @Autowired
    private transient WebTestClient webTestClient;

    @MockBean
    private transient MyService myService;

    @Test
    void shouldReturnResponse() {
        var response = new Response();
        response.id("id");
        response.setAmount(10L);
        when(myService.getResponse()).thenReturn(Mono.just(response));
        webTestClient.post()
                .uri("/")
                .exchange()
                .expectStatus().isOk()
                .expectBody(Response.class)
                .isEqualTo(response);
    }
}

正如您所见,我对myService进行了存根化,使其返回response的Mono,并期望isEqualTo()上相同的结果。

我有一些类似的测试,它们正常工作。我目前正在检查是否与Response对象有关。

英文:

I have a controller unit test using WebTestClient and it's failing on the isEqualTo() test. I can't find what's wrong as I'm using the same object to set what the service call will return and the parameter for isEqualTo()

When I run the test I'm getting something like

Expected :com.model.Response@ab41b46
Actual   :com.model.Response@c1dcdb9

Model: This is just a portion of it but the other objects are just Strings

public class Response implements Serializable {
    private static final long serialVersionUID = 1L; //random long
    private String id;
    private Long amount;
    private LocalDateTime dateTime;
    private String status;

    //setters-getters
}

Controller:

@PostMapping
public Mono&lt;Response&gt; getResponse() {
     return myService.getResponse();
}

Test:

@ExtendWith(MockitoExtension.class)
@WebFluxTest(controllers = MyController.class)
public class MyControllerTest
{
    @Autowired
    private transient WebTestClient webTestClient;

    @MockBean
    private transient MyService myService;

    @Test
    void shouldReturnResponse() {
        var response = new Response();
        response.id(&quot;id&quot;);
        response.setAmount(10L);
        when(myService.getResponse()).thenReturn(Mono.just(response));
        webTestClient.post()
                .uri(&quot;/&quot;)
                .exchange()
                .expectStatus().isOk()
                .expectBody(Response.class)
                .isEqualTo(response);
    }
}

As you can see I stubbed myService to return a Mono of response and just expected the same on isEqualTo()

I have some similar tests to this and they're working fine. I'm currently checking if this is an issue with the Response object

答案1

得分: 1

isEqualTo使用equals方法来验证对象是否相等。

从你在这里发布的内容来看,你没有实现equals方法。没有equals方法意味着它将使用默认的方法,基本上是检查它是否是相同的对象实例。显然这不是真的。

因此,要么实现一个合适的equals方法,要么使用不同的方式来断言你的结果。

@Test
void shouldReturnResponse() {
    var response = new Response();
    response.id("id");
    response.setAmount(10L);
    when(myService.getResponse()).thenReturn(Mono.just(response));
    webTestClient.post()
            .uri("/")
            .exchange()
            .expectStatus().isOk()
            .expectBody(Response.class)
            .consumeWith((response) -> assertThat(response.getId()).isEqualTo("id"));
}

上面的代码片段使用AssertJ来进行断言(当然你也可以扩展这些断言)。要进行其他验证,BodySpec上还有其他方法,选择适合你需求的方法。

英文:

isEqualTo uses the equals method to verify if objects are equal.

Judging from what you have posted here you haven't implemented the equals method. No equals method means it will use the default which is basically checking if it is the same object instance. Which obviously isn't true.

Hence either implement a proper equals method or use a different way of asserting your result.

@Test
void shouldReturnResponse() {
    var response = new Response();
    response.id(&quot;id&quot;);
    response.setAmount(10L);
    when(myService.getResponse()).thenReturn(Mono.just(response));
    webTestClient.post()
            .uri(&quot;/&quot;)
            .exchange()
            .expectStatus().isOk()
            .expectBody(Response.class)
            .consumeWith( (response) -&gt; assertThat(response.getId()).isEqualTo(&quot;id&quot;));}

The snippet above uses AssertJ to do the assert (ofcourse you can extend those as well). To do other validations there are other methods on the BodySpec as well, pick the one that suits your needs.

答案2

得分: 0

你正在创建一个包含响应对象的单一对象。然后,你从你的 WebTestClient 中获取它。你的 isEqualTo 比较了这两个对象,它们并不完全相同,因为 mono -> object。我建议比较对象内部的值。

Mono<Response> clientResponse = webTestClient.post()
                .uri("/")
                .exchange()
                .expectStatus().isOk()
                .expectBody(Response.class);

assertThat(clientResponse.block().getId()).isEqualTo("id");
assertThat(clientResponse.block().getResponse().getAmount()).isEqualTo(10L);
英文:

You're creating a mono object with the response object in it. Then you fetch it from your WebTestClient. You're isEqualTo compares the two Objects, which aren't exactly the same object -> mono -> object. I would suggest to compare the values inside the objects.

Mono&lt;Response&gt; clientResponse = webTestClient.post()
            .uri(&quot;/&quot;)
            .exchange()
            .expectStatus().isOk()
            .expectBody(Response.class);

 assertThat(clientResponse.block().getId()).isEqualTo(&quot;id&quot;);
 assertThat(clientResponse.block().response.getAmount()).isEqualTo(10L);

huangapple
  • 本文由 发表于 2023年6月12日 16:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定