SpringBoot+GraphQL集成测试报错:Body is empty with status 401
问题:SpringBoot + GraphQL集成测试出现401导致NullPointerException
执行JUnit测试用例时出现如下栈追踪错误:
java.lang.NullPointerException: Body is empty with status 401 at java.base/java.util.Objects.requireNonNull(Objects.java:347) at com.graphql.spring.boot.test.GraphQLResponse.<init>(GraphQLResponse.java:24) at com.graphql.spring.boot.test.GraphQLTestTemplate.postRequest(GraphQLTestTemplate.java:142) at com.graphql.spring.boot.test.GraphQLTestTemplate.post(GraphQLTestTemplate.java:137) at com.graphql.spring.boot.test.GraphQLTestTemplate.perform(GraphQLTestTemplate.java:95) at com.graphql.spring.boot.test.GraphQLTestTemplate.postForResource(GraphQLTestTemplate.java:116)
以下是相关配置内容:
pom.xml依赖
<dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphql-spring-boot-starter</artifactId> <version>7.0.1</version> </dependency> <dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphql-java-tools</artifactId> <version>6.0.2</version> </dependency> <dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphql-spring-boot-starter-test</artifactId> <version>7.0.1</version> <scope>test</scope> </dependency>
集成测试代码
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = GraphQLEntryApplication.class) public class PhysicianStorageContractIntegrationTest { @Autowired private GraphQLTestTemplate graphQLTestTemplate; @Test public void shouldGetAllPhysiciansByLocationId() throws IOException, JSONException { var operationName = "GetAllPhysiciansByLocationId"; GraphQLResponse graphqlResponse = graphQLTestTemplate.postForResource(String.format(ContractIntegrationTestUtils.GRAPHQL_QUERY_REQUEST_PATH, operationName)); var expectedResponseBody = ContractIntegrationTestUtils.readResponse(String.format(ContractIntegrationTestUtils.GRAPHQL_QUERY_RESPONSE_PATH, operationName)); Assertions.assertThat(graphqlResponse.getStatusCode()).isEqualTo(HttpStatus.OK); JSONAssert.assertEquals(expectedResponseBody, graphqlResponse.getRawResponse().getBody(), true); } }
集成测试工具类
public final class ContractIntegrationTestUtils { public static final String CONTENT_TYPE_KEY = "Content-Type"; public static final String MEDIATYPE_APP_JSON = "application/json"; public static final String GRAPHQL_QUERY_REQUEST_PATH = "contract-integration-physician-storage/request/%s.graphql"; public static final String GRAPHQL_QUERY_RESPONSE_PATH = "contract-integration-physician-storage/response/%s.json"; public static String readResponse(String responsePath) throws IOException { return IOUtils.toString(new ClassPathResource(responsePath).getInputStream(), UTF_8); } }
GraphQL请求文件
query allPhysiciansByFacilityIdWithNoPagination { allPhysiciansByFacilityIdWithNoPagination(locationId: 13613){ totalSize physicians { firstName lastName middleInitials } } }
响应文件
{ "data": { "allPhysiciansByFacilityIdWithNoPagination": { "totalSize": 4, "physicians": [ { "firstName": "John", "lastName": "Doe", "middleInitials": null }, { "firstName": "Chandler", "lastName": "Bing", "middleInitials": "K" }, { "firstName": "Katy", "lastName": "Perry", "middleInitials": null }, { "firstName": "Ross", "lastName": "Geller", "middleInitials": null } ] } } }
推测该问题和安全配置有关,知道用MockMvc可以禁用安全配置,但不确定使用SpringBoot的GraphQL Test库是否可行,求解决思路或线索。
内容的提问来源于stack exchange,提问作者Marcelo Tataje
相关产品推荐
相关产品推荐

