是否可以将 Mockito 中的某些 return 值列入黑名单?
Is it possible to blacklist certain return values in Mockito?
背景
我正在尝试使用 Mockito 在 class 中测试此方法:
该方法的第一种情况是字符串等于常量。
该方法的第二种情况是字符串等于任何其他内容除了常量。
这是关于 anything except a certain integer 的问题的字符串版本。
public class Class {
private SomeOtherObjectWithAMethod someOtherObjectWithAMethod;
public Class(SomeOtherObjectWithAMethod someOtherObjectWithAMethod){
this.someOtherObjectWithAMethod = someOtherObjectWithAMethod;
}
public void method(){
if(helperObject.obtainAString().equals(HelperObject.A_STRING_CONSTANT)){
someOtherObjectWithAMethod.thisMethod("stringarg");
}
else{
someOtherObjectWithAMethod.thisMethod("differentarg");
}
}
我知道在 Mockito 中你可以
根据
更改mockito中的某些return值(但只有最后一个会生效)
在 thenReturn()
方法中输入 null
作为 return 什么都不做的方法。
- 使用
anyString()
作为虚拟字符串。
- Return 一个布尔值。
部分解决方案
我已经对第一个案例 (str.equals("This string"))
进行了单元测试,如下所示:
private Class instantiatedClass;
@Test
public void testMethod_thisString(){
whenever(helperObject.obtainAString()).thenReturn(HelperObject.A_STRING_CONSTANT);
instantiatedClass.method()
verify(someOtherObjectWithAMethod).thisMethod("stringarg");
}
我打算再写一个类似的测试用例方法。我在下面注释掉了我需要帮助的部分:
@Test
public void testMethod_notThisString(){
whenever(helperObject.obtainAString()).thenReturn(/* A String that is not HelperObject.A_STRING_CONSTANT */);
instantiatedClass.method()
verify(someOtherObjectWithAMethod).thisMethod("differentarg");
}
问题
如何测试任何字符串 除了 的特定值(或多个值)?
您可以查找 creating random strings
并在它们不等于特定值时使用它们。
您可以执行 Mockito.doAnswer( answer )
以更好地控制创建的 String
所以像这样:
List<String> blacklist = Arrays.asList("aaaa","bbbb");
Mockito.doAnswer((i)-> {
String x=RandomStringUtils.random(4);
while(blacklist.contains(x)){
x=RandomStringUtils.random(4);
}
return x;
}).when(helperObject).obtainAsString();
虽然我不知道该怎么做 "any string except certain ones",但这解决了我的问题:
@Test
public void testMethod_notThisString(){
whenever(helperObject.obtainAString()).thenReturn(HelperObject.CONSTANT1, HelperObject.CONSTANT2, HelperObject.CONSTANT3);
instantiatedClass.method()
verify(someOtherObjectWithAMethod).thisMethod("differentarg");
}
这遵循的逻辑。
背景
我正在尝试使用 Mockito 在 class 中测试此方法:
该方法的第一种情况是字符串等于常量。
该方法的第二种情况是字符串等于任何其他内容除了常量。
这是关于 anything except a certain integer 的问题的字符串版本。
public class Class {
private SomeOtherObjectWithAMethod someOtherObjectWithAMethod;
public Class(SomeOtherObjectWithAMethod someOtherObjectWithAMethod){
this.someOtherObjectWithAMethod = someOtherObjectWithAMethod;
}
public void method(){
if(helperObject.obtainAString().equals(HelperObject.A_STRING_CONSTANT)){
someOtherObjectWithAMethod.thisMethod("stringarg");
}
else{
someOtherObjectWithAMethod.thisMethod("differentarg");
}
}
我知道在 Mockito 中你可以
根据
更改mockito中的某些return值(但只有最后一个会生效)
在
thenReturn()
方法中输入null
作为 return 什么都不做的方法。- 使用
anyString()
作为虚拟字符串。 - Return 一个布尔值。
部分解决方案
我已经对第一个案例 (str.equals("This string"))
进行了单元测试,如下所示:
private Class instantiatedClass;
@Test
public void testMethod_thisString(){
whenever(helperObject.obtainAString()).thenReturn(HelperObject.A_STRING_CONSTANT);
instantiatedClass.method()
verify(someOtherObjectWithAMethod).thisMethod("stringarg");
}
我打算再写一个类似的测试用例方法。我在下面注释掉了我需要帮助的部分:
@Test
public void testMethod_notThisString(){
whenever(helperObject.obtainAString()).thenReturn(/* A String that is not HelperObject.A_STRING_CONSTANT */);
instantiatedClass.method()
verify(someOtherObjectWithAMethod).thisMethod("differentarg");
}
问题
如何测试任何字符串 除了 的特定值(或多个值)?
您可以查找 creating random strings
并在它们不等于特定值时使用它们。
您可以执行 Mockito.doAnswer( answer )
以更好地控制创建的 String
所以像这样:
List<String> blacklist = Arrays.asList("aaaa","bbbb");
Mockito.doAnswer((i)-> {
String x=RandomStringUtils.random(4);
while(blacklist.contains(x)){
x=RandomStringUtils.random(4);
}
return x;
}).when(helperObject).obtainAsString();
虽然我不知道该怎么做 "any string except certain ones",但这解决了我的问题:
@Test
public void testMethod_notThisString(){
whenever(helperObject.obtainAString()).thenReturn(HelperObject.CONSTANT1, HelperObject.CONSTANT2, HelperObject.CONSTANT3);
instantiatedClass.method()
verify(someOtherObjectWithAMethod).thisMethod("differentarg");
}
这遵循