How to mock webclient for an get API call

Viewed 88

I am trying to to test a method as shown below but its throwing null pointer exception. I am new to testing and webclient can some one please help me.

Method:

 @Override
    public List<Workspace> modifiedWorkspaces() throws ApplicationException {
        List<Workspace> modifiedWorkspace = webClient.get().uri(URIDetails.MODIFIEDWORKSPACE)
                .header("Authorization", bearerToken).retrieve().bodyToFlux(Workspace.class)
                .collect(Collectors.toList()).block();
        for (Workspace w : modifiedWorkspace) {
            System.out.println("Modified Workspaces:" + w);
        }
        System.out.println("modifiedWorkspace List:" + modifiedWorkspace.size());
        return modifiedWorkspace;
    }

Test method

    @Test
    public void modifiedWorkspaces() throws Exception {
        when(webClient.get().uri(anyString()).retrieve().bodyToFlux(Workspace.class).blockLast())
                .thenReturn("[\"Id\": \"61e2ca91\",\"Id\": \"a19063c4\",\"Id\": \"b6073d2d\",\"Id\": \"e55c1d4e\",\"Id\": \"5cc83328\"]");
        List<WorkspaceID> response = biService.modifiedWorkspaces();
        assertEquals(true, response);
    }
0 Answers
Related