简体   繁体   English

GraphQL:如何为 graphql spqr 和 Z2A2D595E6ED9A0B134F027F2BD6 编写 junit 测试

[英]GraphQL : How to write junit tests for graphql spqr and spring boot

I have implemented GraphQL endpoints with spring boot using the following lib - https://github.com/leangen/graphql-spqr .我已经使用以下库实现了 GraphQL 端点和 spring 引导 - https://github.com/leangen/graphql-spqr I currently have junits for REST endpoints using WebTestClient and have tried writing junits for spqr in a similar way but that doesn't work.我目前使用 WebTestClient 为 REST 端点提供 junit,并尝试以类似的方式为 spqr 编写 junit,但这不起作用。

GraphQL supports POST method for all the endpoints, so does anybody know how to pass query and mutation requests with web test client OR Is there any other way to write junits for spqr lib? GraphQL 支持所有端点的 POST 方法,所以有人知道如何使用 web 测试客户端传递查询和变异请求,或者有没有其他方法可以为 spqr lib 编写 junit?

What is it that doesn't work?什么是行不通的? GraphQL requests are just normal HTTP requests with (usually) a JSON body, nothing special. GraphQL 请求只是普通的 HTTP 请求(通常)带有 JSON 主体,没什么特别的。

Just POST a JSON with the following fields:只需发布带有以下字段的 JSON :

  • query - the query/mutation/subscription query - 查询/突变/订阅
  • operationName - only needed if you're posting multiple queries at once operationName - 仅当您同时发布多个查询时才需要
  • variables - an object with variables eg {'name': 'value'} variables - 带有变量的 object 例如{'name': 'value'}

You can also pass these as GET params.您也可以将这些作为 GET 参数传递。 Check the GraphQL over HTTP spec for more details and examples.查看GraphQL 而非 HTTP 规范以获取更多详细信息和示例。

You can use JsonPath to make assertions on the result then.然后,您可以使用JsonPath对结果进行断言。

assertEquals("John Doe", JsonPath.read(result, "$.data.book[0].author"));

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

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