3

我正在尝试使用 Spring Boot 为 GraphQl 编写 JUnit 测试。我的测试在以下方法中失败。我在treeWiring.abcFetcher收到AsserException。(引起:graphql.AssertException:您必须提供数据提取器)。请帮助我如何模拟数据获取器。它的值显示为空。

private RuntimeWiring buildWiring() {
            return RuntimeWiring.newRuntimeWiring()
                    .type(newTypeWiring("Query")
                            .dataFetcher("xList", abcWiring.abcDataFetcher));

下面是我的源类

@Component
public class GraphQLProvider {
    private GraphQL graphQL;
    private TreeWiring abcWiring;




    @Autowired
    public GraphQLProvider(TreeWiring abcWiring) {
        this.abcWiring = abcWiring;
    }

    @PostConstruct
    public void init() throws IOException {
        URL url = Resources.getResource("graphql/x.graphqls");
        String sdl = Resources.toString(url, Charsets.UTF_8);
        GraphQLSchema graphQLSchema = buildSchema(sdl);

        Instrumentation instrumentation = new TracingInstrumentation();

        DataLoaderRegistry registry = new DataLoaderRegistry();
        DataLoaderDispatcherInstrumentation dispatcherInstrumentation
                = new DataLoaderDispatcherInstrumentation(registry);

        this.graphQL = GraphQL.newGraphQL(graphQLSchema).instrumentation(instrumentation).instrumentation(dispatcherInstrumentation).build();
    }

    private GraphQLSchema buildSchema(String sdl) {
        TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
        RuntimeWiring runtimeWiring = buildWiring();
        SchemaGenerator schemaGenerator = new SchemaGenerator();
        return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    }

    private RuntimeWiring buildWiring() {
        return RuntimeWiring.newRuntimeWiring()
                .type(newTypeWiring("Query")
                        .dataFetcher("xList", abcWiring.abcDataFetcher));

下面是我的TestCLass:

@ContextConfiguration(classes = GraphQLProvider.class)
@SpringBootTest
@RunWith(SpringRunner.class)
public class GraphQLProviderTest {


    @MockBean
    TreeWiring treeWiring;


    @MockBean
    DataFetcher abcDataFetcher;

    //@InjectMocks
    //GraphQLProvider graphQLProvider = new GraphQLProvider(treeWiring);

    @Autowired
    @InjectMocks
    GraphQLProvider graphQLProvider;



    @Before
      public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testGetUserLabels() throws IOException  {
        Mockito.when(treeWiring.db2HealthDataFetcher).thenReturn(db2HealthDataFetcher);
        graphQLProvider.init();
    }


}

下面是我的 TreeWiring 代码:

@Component
public class TreeWiring {

@Autowired
    public TreeWiring(OneRepository oneRepository,
            TwoRepository twoRepository,
            ThreeRepository threeRepository,
            FourRepository fourRepository) {
        this.oneRepository = oneRepository;
        this.twoRepository = twoRepository;
        this.threeRepository =threeRepository;
        this.fourRepository = fourRepository;
    }


DataFetcher abcDataFetcher = environment -> {   

        String eID = environment.getArgument("eId");
        String dID = environment.getArgument("dId");

return oneRepository.getUserLabel(eID);
4

0 回答 0