Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ClientRequestBase mock friendly #9281

Merged
merged 2 commits into from
Sep 27, 2024

Conversation

jbescos
Copy link
Member

@jbescos jbescos commented Sep 24, 2024

Description

I was mocking WebClient and I found some issues related to null pointers and unable to mock relevant methods.

This was the method I had:

    private WebClient mockWebClient(Status status) {
        WebClient webClient = Mockito.mock(WebClient.class);
        HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
        HttpClientResponse response = Mockito.mock(HttpClientResponse.class);
        doReturn(request).when(webClient).post(anyString());
        doReturn(request).when(request).header(any(HeaderName.class), anyString());
        doReturn(response).when(request).submit(any(Object.class));
        doReturn(OpenAiChatResponse.create(null, null, 0, null, null, null, List.of(), null)).when(response).as(OpenAiChatResponse.class);
        when(response.status()).thenReturn(status);
        return webClient;
    }

Some issues:

java.lang.NullPointerException: Cannot invoke "io.helidon.http.Method.equals(Object)" because "this.method" is null
	at io.helidon.webclient.api.ClientRequestBase.rejectHeadWithEntity(ClientRequestBase.java:484)
	at io.helidon.webclient.api.ClientRequestBase.submit(ClientRequestBase.java:267)
	at io.helidon.ai.openai.OpenAiChatClientTest.mockWebClient(OpenAiChatClientTest.java:66)
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at io.helidon.ai.openai.OpenAiChatClientTest.mockWebClient(OpenAiChatClientTest.java:67)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

	at io.helidon.webclient.api.ClientRequestBase.submit(ClientRequestBase.java:269)
	at io.helidon.ai.openai.OpenAiChatClientTest.mockWebClient(OpenAiChatClientTest.java:67)

Documentation

NA

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Sep 24, 2024
@jbescos jbescos self-assigned this Sep 24, 2024
Signed-off-by: Jorge Bescos Gascon <[email protected]>
@jbescos jbescos force-pushed the requestMockFriendly branch from 650633f to 0f6d2d3 Compare September 24, 2024 10:27
Copy link
Contributor

@romain-grecourt romain-grecourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test.

@jbescos
Copy link
Member Author

jbescos commented Sep 25, 2024

I have added a test. This is without the head fix:

[ERROR] io.helidon.webclient.tests.WebClientMockTest.useMock  Time elapsed: 0.287 s  <<< ERROR!
java.lang.NullPointerException: Cannot invoke "io.helidon.http.Method.equals(Object)" because "this.method" is null
	at io.helidon.webclient.api.ClientRequestBase.rejectHeadWithEntity(ClientRequestBase.java:484)
	at io.helidon.webclient.api.ClientRequestBase.submit(ClientRequestBase.java:267)
	at io.helidon.webclient.tests.WebClientMockTest.mockWebClient(WebClientMockTest.java:54)
	at io.helidon.webclient.tests.WebClientMockTest.useMock(WebClientMockTest.java:41)

And this is without the final remove:

[ERROR] io.helidon.webclient.tests.WebClientMockTest.useMock  Time elapsed: 0.296 s  <<< ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at io.helidon.webclient.tests.WebClientMockTest.mockWebClient(WebClientMockTest.java:54)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

	at io.helidon.webclient.api.ClientRequestBase.submit(ClientRequestBase.java:269)
	at io.helidon.webclient.tests.WebClientMockTest.mockWebClient(WebClientMockTest.java:54)
	at io.helidon.webclient.tests.WebClientMockTest.useMock(WebClientMockTest.java:41)

@tomas-langer @romain-grecourt could you approve this before the new release of Helidon?. We need it for AI.

Signed-off-by: Jorge Bescos Gascon <[email protected]>
@jbescos jbescos force-pushed the requestMockFriendly branch from 9296e0f to 0e9e9a2 Compare September 25, 2024 05:30
@jbescos jbescos merged commit 7edf00b into helidon-io:main Sep 27, 2024
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants