Mockito 'when' 带有 JPA 存储库和 ignoreCase finder 方法
Mockito 'when' with JPA Repository with ignoreCase finder method
我正在尝试模拟不区分大小写的用户存储库...并模拟 returns 不应该的对象...
Pattern adminPattern = Pattern.compile(Pattern.quote("admin"), Pattern.CASE_INSENSITIVE);
Pattern admin2Pattern = Pattern.compile(Pattern.quote("admin2"), Pattern.CASE_INSENSITIVE);
Mockito.when(userRepo.findByUserNameIgnoreCase(Mockito.matches(adminPattern))).thenReturn(user1);
Mockito.when(userRepo.findByUserNameIgnoreCase(Mockito.matches(admin2Pattern))).thenReturn(user2);
Assert.isTrue(adminPattern.matcher("admin").matches(), "admin should match");
Assert.isTrue(adminPattern.matcher("adMIN").matches(), "adMIN should match");
Assert.isTrue(admin2Pattern.matcher("admin2").matches(), "admin2 should match");
Assert.isTrue(admin2Pattern.matcher("adMIN2").matches(), "adMIN2 should match");
Assert.isTrue(!adminPattern.matcher("admin3").matches(), "admin3 should not match");
Assert.isTrue(!admin2Pattern.matcher("adMIN").matches(), "adMIN should not match");
Assert.isTrue(!admin2Pattern.matcher("adMIN3").matches(), "adMIN3 should not match");
Assert.isTrue(userRepo.findByUserNameIgnoreCase("admin").equals(user1), "Admin must be found");
Assert.isTrue(userRepo.findByUserNameIgnoreCase("adMIN").equals(user1), "Admin must be found");
Assert.isNull(userRepo.findByUserNameIgnoreCase("anything"), "anything must not be found");
Assert.isNull(userRepo.findByUserNameIgnoreCase("admin3"), "Admin3 must not be found");
最后一行出错了... Mockito returns user1 而不是什么都没有。
在我看来 'matches' 实际上是一个 'startsWith' 这让我有点惊讶......
或者我的正则表达式模式匹配器是错误的......或者我错过了其他一些非常明显的东西(对其他人:-))
非常感谢您的一些想法!
如果您查看 org.mockito.internal.matchers.Matches
class,
您会注意到它在正则表达式模式匹配器上调用 find
,而不是 matches
。
@Override
public boolean matches(Object actual) {
return (actual instanceof String) && pattern.matcher((String) actual).find();
}
区别是:
find
查找输入字符串中出现的模式
matches
尝试将整个输入与模式匹配。
要强制匹配整个输入,您可以稍微修改您的模式:"^admin$"
我正在尝试模拟不区分大小写的用户存储库...并模拟 returns 不应该的对象...
Pattern adminPattern = Pattern.compile(Pattern.quote("admin"), Pattern.CASE_INSENSITIVE);
Pattern admin2Pattern = Pattern.compile(Pattern.quote("admin2"), Pattern.CASE_INSENSITIVE);
Mockito.when(userRepo.findByUserNameIgnoreCase(Mockito.matches(adminPattern))).thenReturn(user1);
Mockito.when(userRepo.findByUserNameIgnoreCase(Mockito.matches(admin2Pattern))).thenReturn(user2);
Assert.isTrue(adminPattern.matcher("admin").matches(), "admin should match");
Assert.isTrue(adminPattern.matcher("adMIN").matches(), "adMIN should match");
Assert.isTrue(admin2Pattern.matcher("admin2").matches(), "admin2 should match");
Assert.isTrue(admin2Pattern.matcher("adMIN2").matches(), "adMIN2 should match");
Assert.isTrue(!adminPattern.matcher("admin3").matches(), "admin3 should not match");
Assert.isTrue(!admin2Pattern.matcher("adMIN").matches(), "adMIN should not match");
Assert.isTrue(!admin2Pattern.matcher("adMIN3").matches(), "adMIN3 should not match");
Assert.isTrue(userRepo.findByUserNameIgnoreCase("admin").equals(user1), "Admin must be found");
Assert.isTrue(userRepo.findByUserNameIgnoreCase("adMIN").equals(user1), "Admin must be found");
Assert.isNull(userRepo.findByUserNameIgnoreCase("anything"), "anything must not be found");
Assert.isNull(userRepo.findByUserNameIgnoreCase("admin3"), "Admin3 must not be found");
最后一行出错了... Mockito returns user1 而不是什么都没有。 在我看来 'matches' 实际上是一个 'startsWith' 这让我有点惊讶...... 或者我的正则表达式模式匹配器是错误的......或者我错过了其他一些非常明显的东西(对其他人:-))
非常感谢您的一些想法!
如果您查看 org.mockito.internal.matchers.Matches
class,
您会注意到它在正则表达式模式匹配器上调用 find
,而不是 matches
。
@Override
public boolean matches(Object actual) {
return (actual instanceof String) && pattern.matcher((String) actual).find();
}
区别是:
find
查找输入字符串中出现的模式matches
尝试将整个输入与模式匹配。
要强制匹配整个输入,您可以稍微修改您的模式:"^admin$"