Play Framework Java: Mock using Mockito: Wanted but not invoked, Actually, there were zero interactions with this mock

ぐ巨炮叔叔 提交于 2021-02-04 08:36:09

问题


I am new to Mockito and Play Framework, so can anyone suggest a solution for this:
I have 3 classes as follows:
I have a Play application where I am calling a helper method, which indeed calls an ApiClient, which calls an external api and fetches the results and returns CompletionStage of that result. And the helper method indeed returns the same result to the controller.
The Code is as follows.

public class MyController extends Controller{
    @Inject
    WSClient wsClient;

    MyControllerHelper myControllerHelper;

    public CompletionStage<Result> myMethod(){
         myControllerHelper = new MyControllerHelper(wsClient);
         myControllerHelper.helperMethod("searchWord");
    }
}

public class MyControllerHelper {
     WSClient wsClient;

     ApiClient apiClient;

     public MyControllerHelper(WSClient wsClient) {
         apiClient = new ApiClient(wsClient);
     }
     public CompletionStage<SearchResults> helperMethod(String searchWord){
         return apiClient.fetchMethod(searchWord);
     }
}

class ApiClient{
    WSClient wsClient;

    public ApiClient(WSClient wsClient){
       this.wsClient = wsClient;
    }
    public CompletionStage<SearchResults> fetchMethod(String searchWord){
    }
}

To Mock the Controller:

public class MyControllerTest extends WithApplication {

    @Override
    protected Application provideApplication() {
        return new GuiceApplicationBuilder().build();
    }

    MyControllerHelper myControllerHelperSpy;

    @Test
    public void testIndexWithSearchWords() {
        Http.RequestBuilder request = new Http.RequestBuilder()
                .method(POST)
                .uri("/");
        request.session(SessionHelper.SESSION_KEY, SessionHelper.SESSION_KEY);
        request.header("USER_AGENT", "");
        myControllerHelperSpy = spy(new MyControllerHelper());
        doReturn(CompletableFuture.supplyAsync(SearchResults::new)).when(myControllerHelperSpy).fetchMethod(anyString());
        verify(myControllerHelperSpy).fetchMethod(anyString());
        Map<String, String[]> requestBody = new HashMap<>();
        String[] searchKeyWord = new String[]{"hello world"};
        requestBody.put("searchKeyword", searchKeyWord);
        request.bodyFormArrayValues(requestBody);
        Result result = route(app, request);
        assertEquals(OK, result.status());
    }
}

When I run the tests, it throws error:

Wanted but not invoked:
myControllerHelperSpy.fetchMethod(<any string>);
-> at controllers.MyController.testIndexWithSearchWords(MyControllerHelper.java:52)
Actually, there were zero interactions with this mock.

Can anyone suggest a solution to this? How to test this hierarchy with mockito? Thank You

来源:https://stackoverflow.com/questions/64636980/play-framework-java-mock-using-mockito-wanted-but-not-invoked-actually-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!