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

Possible race condition #3280

Open
Luqheltod opened this issue Feb 23, 2024 · 3 comments
Open

Possible race condition #3280

Luqheltod opened this issue Feb 23, 2024 · 3 comments

Comments

@Luqheltod
Copy link

Having a mock of a service with 7 params ( I think a high number of params is somehow important ) and then mocking another one, I am getting a exception that doesnt have any sense.
If u switch the position and mock the one with a lot of params the last, the exception never happens.
If u debug the program, the exception never happens.

Here is the sample and the code and the exception:

when(myMokedService.getSomething(any(),any(),any(),any(),any(),any(),any())).thenReturn(any(Timestamp.class));
when(myAnotherMokedService.getSomething(anyString())).thenReturn(anyMockedResponse);

EXCEPTION ->

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
-> at service.automatism.AutomatismSearchExecServiceTest.givenSearchExec_whenUpdateSearchExec_then_sameSearchExecVariablesNotNull(AutomatismSearchExecServiceTest.java:121)
-> at service.automatism.AutomatismSearchExecServiceTest.givenSearchExec_whenUpdateSearchExec_then_sameSearchExecVariablesNotNull(AutomatismSearchExecServiceTest.java:123)

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

@jkim323
Copy link

jkim323 commented Feb 27, 2024

Hi, if you haven't yet found the root cause to your issue I would like to help take a stab at it. Based on what I read and understood so far, seems like the error message is indicating that you are mixing matchers with raw values, which is causing the problem. Have you tried providing a specific value for the getSomething method? I think the error would be resolved when you use the matchers consistently for all the arguments in your mock setup.

@Luqheltod
Copy link
Author

Hello @jkim323 , yes I have tried that already. To keep working I just switched the positions of the mocks but i thoight that maybe this can be easily fixed by someone who knows the library.And yes you are right, the error not happens if you dont use matchers(a real value) but still happens if you use any(some.class) or eq(myString)

@jkim323
Copy link

jkim323 commented Mar 3, 2024

Hm ok. From my own experience and what I know of, I think the problem is because you can't use argument matcher and raw values together based on the error info you provided. Therefore that is why you don't see an error when you run someMethod(any(), eq("String by matcher")). Either you should make all arguments as raw or have all arguments using matcher. I think what can help is if you can provide a snippet of your method that might help also!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants