I have test method using mockito 5
@Test
public void test_increaseUserScore_Successful() throws CannotCreateCachedUserException {
when(cachedUserRepository
.existsById(anyLong())
).thenReturn(true);
when(leaderboardInfoRepository
.findById(anyString())
).thenReturn(Optional.of(testLeaderboardInfo));
when(cachedUserRepository
.findById(anyLong())
).thenReturn(Optional.of(userCached));
when(stringRedisTemplate
.execute(
eq(mutableLeaderboardScript), //RedisScript
anyList(), // List with Keys
any(Object.class) // Object args... which does not match
)
).thenReturn("success");
leaderboardService.increaseUserScore(userScoreEvent);
verify(stringRedisTemplate, times(1))
.execute(eq(mutableLeaderboardScript), anyList(), any(Object.class));
}
Here is the problem stringRedisTemplate of execute has signature(RedisScript script, List keys, Object args...) i cannot match args... to any(Object.class).
I either take exception PotentialStubbingProblem:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'execute' method:
stringRedisTemplate.execute(
mutableLeaderboardScript,
[user_cached:11:dailyAttempts, user_cached:11:totalAttempts, leaderboard:lb-123:mutable, user_cached:11, global-leaderboard-stream, local-leaderboard-stream],
"11",
"10.0",
"5",
"2",
"[ZZ]",
"20",
"lb-123",
"ZZ"
);
- has following stubbing(s) with different arguments:
1. stringRedisTemplate.execute(null, [], null);
or null, which depends on lenient annotation, the same result will be given if i replace any() on ArgumentMatchers.<Object'>'any(), any(Object[].class) and (Object[]) any()
-
You question will benefit from MRE. As of now it id unclear which method you talking about and what is it signature.talex– talex2025年11月24日 07:40:40 +00:00Commented 17 hours ago
-
Take a look at stackoverflow.com/q/2631596/112968 and github.com/mockito/mockito/issues/2796knittl– knittl2025年11月24日 09:34:14 +00:00Commented 15 hours ago
-
@knittl i actually tried it in the code. I have any(Object.class) and I wrote that the same result is given if i replace any() on ArgumentMatchers.<Object'>'any(), any(Object[].class) and (Object[]) any()JZARGO– JZARGO2025年11月24日 11:04:09 +00:00Commented 13 hours ago
-
@talex I’ve corrected it. Is it clear now?JZARGO– JZARGO2025年11月24日 11:05:57 +00:00Commented 13 hours ago
1 Answer 1
It always helps to write a MRE (minimal, reproducible example). It also makes it easier for people trying to help you, because they can simply copy-past your code and adapt as needed. Let's do that for your code:
- Get rid of all the repositories that are not required to show the problem
- Remove arguments that are unrelated to the problem
- Implement a super simple implementation of your service
That leaves you with 2 interfaces and 1 implementation:
interface LeaderboardService {
void increaseUserScore();
}
interface StringRedisTemplate {
String execute(List<String> keys, Object... args);
}
class LeaderboardServiceImpl implements LeaderboardService {
private final StringRedisTemplate stringRedisTemplate;
LeaderboardServiceImpl(final StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
@Override
public void increaseUserScore() {
final String result = stringRedisTemplate.execute(
List.of("some key"),
"arg1", "arg2" // varargs call
);
if (!Objects.equal(result, "success")) {
throw new RuntimeException("Unexpected result");
}
}
}
And then write the test. You will note that you can in fact match varargs calls with any(Object[].class) (because Object args... is basically syntactic sugar for Object[] args). At least it does with Mockito 5.16.1.
If you need to match an exact number of arguments, pass as many matchers as required.
Here's the test that passes, using both styles of matchers:
@Mock
StringRedisTemplate stringRedisTemplate;
@Test
void test_increaseUserScore_Successful() {
final LeaderboardService leaderboardService = new LeaderboardServiceImpl(stringRedisTemplate);
when(stringRedisTemplate
.execute(
anyList(), // List with Keys
any(Object[].class) // Object args... which is syntactic suga for Object[] args
)
).thenReturn("success");
leaderboardService.increaseUserScore();
// match varags with explicit array matcher:
verify(stringRedisTemplate).execute(anyList(), any(Object[].class));
// match varags with exact number of arguments:
verify(stringRedisTemplate).execute(anyList(), any(Object.class), any(Object.class));
}
Comments
Explore related questions
See similar questions with these tags.