简体   繁体   English

GraphQLTestTemplate 没有自动装配

[英]GraphQLTestTemplate not getting autowired

I am using GraphQLTestTemplate to mock responses for queries.我正在使用 GraphQLTestTemplate 来模拟查询的响应。

@RunWith(SpringRunner.class)
@GraphQLTest
public class UnitTest {

    @Autowired
    private GraphQLTestTemplate graphQlTestTemplate ;
}

When i run unit test it is giving me error : org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.graphql.spring.boot.test.GraphQLTestTemplate' available: expected at least 1 bean which qualifies as autowire candidate.当我运行单元测试时,它给了我错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.graphql.spring.boot.test.GraphQLTestTemplate' available: expected at least 1 bean which qualifies as autowire candidate.

pom.xml: pom.xml:

        <graphql-spring-boot-starter-test.version>5.0.2</graphql-spring-boot-starter-test.version>
        <graphql-java-tools.version>5.2.4</graphql-java-tools.version>

Before giving you a working snippet we must clarify a few things.在给你一个工作片段之前,我们必须澄清一些事情。

  • I'm using : graphql-spring-boot-starter and我正在使用: graphql-spring-boot-starter
    graphql-spring-boot-starter-test , both of version 6.0.0 . graphql-spring-boot-starter-test ,均为6.0.0版本。

    The latter is embedding junit 5 so you might not need to use need to use @RunWith后者嵌入了junit 5因此您可能不需要使用需要使用@RunWith

  • GraphQLTest is loading only a sliced context of your application, which are the beans related to GraphQL, this is to say, in your tests you should mock the beans that you are using under the hood, like a service your resolvers are using for example. GraphQLTest 仅加载应用程序的切片上下文,即与 GraphQL 相关的 bean,也就是说,在您的测试中,您应该模拟您在后台使用的 bean,例如解析器正在使用的服务。

With that said: here is my working test example, Hope it helps.话虽如此:这是我的工作测试示例,希望它有所帮助。

@GraphQLTest
public class UserQueryIntTest {


    @Autowired
    private GraphQLTestTemplate graphQLTestTemplate;

    @MockBean
    UserService userServiceMock;


    @Test
    @WithMockUser(username = TEST_USERNAME)
    public void getUser() throws Exception {

        User user = new User();
        user.setUsername(TEST_USERNAME);
        user.setPassword(TEST_PASSWORD);
        doReturn(user).when(userServiceMock).getUser(TEST_USERNAME, TEST_PASSWORD);

        GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/get-user.graphql");
        assertThat(response.isOk()).isTrue();
        assertThat(response.get("$.data.getUser.id")).isNotNull();
        assertThat(response.get("$.data.getUser.username")).isEqualTo(TEST_USERNAME);
    }
}

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

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