java.lang.AssertionError:预期状态:<200 OK>,但对于WebFluxTest为:<404 NOT_FOUND>

时间:2019-12-17 07:55:30

标签: spring-boot testing mockito project-reactor mockmvc

我正在尝试为HTTP请求编写Spring响应测试,但出现以下错误:

java.lang.AssertionError:预期状态:<200 OK>,但是是:<404 NOT_FOUND>

我的ReservationHttpTest如下:

package com.example.producer;    
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Flux;

@WebFluxTest(ReservationHttpTest.class)
@RunWith(SpringRunner.class)
public class ReservationHttpTest {


    @MockBean
    private ReservationRepository repository;

    @Autowired
    private WebTestClient client;// Requires @WebFLuxTest

    @Test
    public void getAllReservations() throws Exception{


        Mockito.when(this.repository.findAll())
                .thenReturn(Flux.just(new Reservation("1","Jane"), new Reservation("2","Joe")));


        this.client
            .get()
            .uri("/reservations")
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
            .expectBody().jsonPath("@.[0].name").isEqualTo("Jane")
                         .jsonPath("@.[1].name").isEqualTo("Joe");


    }
}

这是我的错误日志:

java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>

> GET /reservations
> WebTestClient-Request-Id: [1]

No content

< 404 NOT_FOUND Not Found
< Content-Type: [application/json]
< Content-Length: [138]

{"timestamp":"2019-12-17T07:51:03.687+0000","path":"/reservations","status":404,"error":"Not Found","message":null,"requestId":"7c96232e"}


    at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:209)
    at org.springframework.test.web.reactive.server.StatusAssertions.assertStatusAndReturn(StatusAssertions.java:227)
    at org.springframework.test.web.reactive.server.StatusAssertions.isOk(StatusAssertions.java:67)
    at com.example.producer.ReservationHttpTest.getAllReservations(ReservationHttpTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
...

0 个答案:

没有答案