【问题标题】:GraphQLTestTemplate not getting autowiredGraphQLTestTemplate 没有自动装配
【发布时间】:2020-06-03 23:43:12
【问题描述】:

我正在使用 GraphQLTestTemplate 来模拟查询的响应。

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

    @Autowired
    private GraphQLTestTemplate graphQlTestTemplate ;
}

当我运行单元测试时,它给了我错误: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:

        <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>

【问题讨论】:

    标签: spring spring-boot graphql-spring-boot


    【解决方案1】:

    在给你一个有效的 sn-p 之前,我们必须澄清一些事情。

    • 我正在使用: graphql-spring-boot-starter
      graphql-spring-boot-starter-test 都是6.0.0 版本。

      后者是嵌入junit 5,所以你可能不需要使用需要使用@RunWith

    • GraphQLTest 仅加载应用程序的切片上下文,它们是与 GraphQL 相关的 bean,也就是说,在您的测试中,您应该模拟您在后台使用的 bean,就像您的服务一样例如,解析器正在使用。

    话虽如此:这是我的工作测试示例,希望对您有所帮助。

    @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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 2013-07-28
      • 2014-04-19
      相关资源
      最近更新 更多