gpt4 book ai didi

spring-boot - GraphQL 测试返回 404 未找到

转载 作者:行者123 更新时间:2023-12-04 03:48:28 27 4
gpt4 key购买 nike

我有一个使用 GraphQL 的 Spring Boot 应用程序。我想使用 Spring Boot GraphQL 测试库添加集成测试。我正在尝试以下测试:

@GraphQLTest
public class UserTest {
@Autowired
private GraphQLTestTemplate graphQLTestTemplate;

@Test
public void createUser() throws IOException {
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/register.graphql");
assertNotNull(response);
assertTrue(response.isOk());
assertEquals("1", response.get("$.data.post.id"));
}
}

我得到以下响应:

<404,<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> The requested resource [&#47;api&#47;graphql] is not available</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.37</h3></body></html>,[Content-Type:"text/html;charset=utf-8", Content-Language:"en", Content-Length:"766", Date:"Sun, 08 Nov 2020 00:18:11 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>

这是相关的 GitHub 问题: https://github.com/graphql-java-kickstart/graphql-spring-boot/issues/267

感谢任何帮助...

最佳答案

我遇到了同样的问题。删除 @GraphQLTest 并添加以下代码。尽管 GraphQLTest 接口(interface)具有 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

,但我不确定为什么这会起作用
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserTest {
@Autowired
private GraphQLTestTemplate graphQLTestTemplate;

@Test
public void createUser() throws IOException {
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/register.graphql");
assertNotNull(response);
assertTrue(response.isOk());
assertEquals("1", response.get("$.data.post.id"));
}
}

关于spring-boot - GraphQL 测试返回 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64752857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com