首页 > 解决方案 > /graphql 端点在 spring 云应用程序的集成测试期间返回 404 错误,但不是通过使用 mvn spring-boot:run 运行应用程序

问题描述

spring-cloud.version:Greenwich.SR2,spring boot 2.1.7

mvn spring-boot:run 使端点 /graphql 可访问,但不能 mvn clean verify

我包括了 graphql-java-servlet、javax.servlet-api、graphql-spring-boot-starter-test 和 graphql-spring-boot-starter,但它仍然不起作用

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- graphql -->
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java</artifactId>
        <version>13.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-extended-scalars</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>java-dataloader</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>5.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <version>5.0.2</version>
    </dependency>

    <!-- Apache CXF -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.10.0</version>
    </dependency>

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>spring-mock-mvc</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>
    </dependency>

这是我返回 404 的简单测试:

@Test
public void persons() throws Exception {

    Map<String, Object> variables = new HashMap<String, Object>();

    GraphQLRequest request = new GraphQLRequest(
            "query persons{persons"
                    + "{"
                    + "     id"
                    + "}}",
            variables,
            null);

    // SUT
    given()
            .contentType(ContentType.JSON)
            .body(request)
            .get(GRAPHQL_PATH)
            .then()
            .log().body()
            .statusCode(200);
}

测试类注释为:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:bootstrap-test.yml")

这是我的 bootstrap.yml:

spring:
  application:
    name: crm-service
  cloud:
    config:
      uri: http://localhost:8081
      fail-fast: false
      password: configPassword
      username: user
  main:
    allow-bean-definition-overriding: true #i dont remember why but i think there is a bug with spring cloud and OAuth2ClientContext

acls-management:
  permissions-config-path: permissions-config.json
  acl-rules-path: acl-rules.json

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

graphql:
  servlet:
    mapping: /graphql
    enabled: true

标签: spring-bootgraphqlintegration-testingrest-assured

解决方案


推荐阅读