控制器的Micronaut Spock功能测试抛出404错误

时间:2019-06-29 03:50:28

标签: micronaut

尝试使用spock获取简单的问候语控制器时遇到404错误。

我尝试将RxHttpClient修改为RxStreamingHttpClient

My Greetings Controller endpoint:    
@Get("/greetings/{name}")
    String greetings(String name){
        return "hello "+name;
    }

Spock测试:

class TacRulesControllerSpec extends Specification {

    @Shared
    @AutoCleanup
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)

    @Shared
    @AutoCleanup
    RxStreamingHttpClient client = embeddedServer.applicationContext.createBean(RxStreamingHttpClient, embeddedServer.getURL())



    def "greetings controller check" () {

        when:
        HttpRequest httpRequest = HttpRequest.GET("/greetings/bon")
        HttpResponse<String> httpResponse = client.toBlocking().exchange(httpRequest)
        then:
        httpResponse.status == HttpStatus.OK
    }

}
  1. 我希望该测试能够通过,但会收到404错误
  2. 如果API(在这种情况下为/greetings/bon)抛出错误,为什么它没有传播并打印在日志中?我已经看到了实际的API引发400错误的情况,但是micronaut提供的声明性httpclient引发404错误。
    22:27:42.889 [nioEventLoopGroup-1-3] DEBUG i.m.http.client.DefaultHttpClient - Sending HTTP Request: GET /greetings/bon
    22:27:42.889 [nioEventLoopGroup-1-3] DEBUG i.m.http.client.DefaultHttpClient - Chosen Server: localhost(19174)
    22:27:42.891 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - X-B3-TraceId: 4b31b84f1f36b856
    22:27:42.891 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - X-B3-SpanId: 4b31b84f1f36b856
    22:27:42.891 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - X-B3-Sampled: 0
    22:27:42.891 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - host: localhost:19174
    22:27:42.891 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - connection: close
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - HTTP Client Response Received for Request: GET http://localhost:19174/greetings/bon
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Status Code: 404 Not Found
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Date: Sat, 29 Jun 2019 03:27:42 GMT
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - content-type: application/json
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - content-length: 90
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - connection: close
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Response Body
    22:27:43.009 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - ----
    22:27:43.010 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - {"_links":{"self":{"href":"/greetings/bon","templated":false}},"message":"Page Not Found"}
    22:27:43.010 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - ----

    io.micronaut.http.client.exceptions.HttpClientResponseException: Page Not Found

        at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1783)
        at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1723)
        at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at i

1 个答案:

答案 0 :(得分:0)

enter image description here我可以通过从Terminal运行(仍然无法从IntelliJ运行)来解决此问题。

void "Greetings check"() {
    given:
    String response = client.toBlocking().retrieve("/greetings/bon")

    expect:
    response.contains("bon")
}

看起来是groovy 2.5及更高版本所需的。

./ gradlew测试正在运行,但是intelliJ的测试执行却无法正常工作。