Mockito 间谍 return 模拟对象 org.mockito.exceptions.misusing.WrongTypeOfReturnValue
Mockito spy return mock object with org.mockito.exceptions.misusing.WrongTypeOfReturnValue
我正在尝试使用 Mockito 测试以下代码:
class Item {
String name;
int age;
String getInfo() {
return name + age.toString();
}
}
class Article {
Item item = new Item();
Article() {
}
void calculate() {
item = prepareItem();
String info = item.getInfo();
println("The info is " + info);
}
Item prepareItem() {
item.name = "item";
return item;
}
我正在尝试测试方法 calculate()
,这是测试用例:
@Test
public void testCalculate() {
@Mock Item item;
Article article = new Article();
Article articleSpy = Mockito.spy(article);
Mockito.doReturn(item).when(articleSpy).prepareItem();
article.calculate();
}
然后我得到了以下信息:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Item$$EnhancerByMockitoWithCGLIB$ffb1868 cannot be returned by getMetaClass()
getMetaClass() should return MetaClass
可以监视 return 模拟对象吗?这个错误的原因是什么?以及如何修复它?
谢谢!
@Mock
不适用于局部变量。为什么你仍然需要模拟?我认为您不需要嘲笑该项目。只需根据您的需要创建一个测试对象 Item
。模拟是为了防止真正的方法被调用。
试试这个:
@Test
public void testCalculate() {
Item item = new Item();
Article article = new Article();
Article articleSpy = Mockito.spy(article);
Mockito.doReturn(item).when(articleSpy).prepareItem();
article.calculate();
}
如果您真的需要间谍 return Mock,Item item = Mockito.mock(Item.class);
应该可以在 Mocktio 1.10 中使用。我不建议您这样做,因为在大多数情况下不需要这样做。大多数时候您可以简化代码(=> 测试驱动开发)。
如果您绝对需要它,这将是一个有效的示例:
Item item = Mockito.mock(Item.class);
Mockito.when(item.getInfo()).thenReturn("bla bla");
Article article = Mockito.spy(Article.class);
Mockito.doReturn(item).when(article).prepareItem();
article.calculate();
我正在尝试使用 Mockito 测试以下代码:
class Item {
String name;
int age;
String getInfo() {
return name + age.toString();
}
}
class Article {
Item item = new Item();
Article() {
}
void calculate() {
item = prepareItem();
String info = item.getInfo();
println("The info is " + info);
}
Item prepareItem() {
item.name = "item";
return item;
}
我正在尝试测试方法 calculate()
,这是测试用例:
@Test
public void testCalculate() {
@Mock Item item;
Article article = new Article();
Article articleSpy = Mockito.spy(article);
Mockito.doReturn(item).when(articleSpy).prepareItem();
article.calculate();
}
然后我得到了以下信息:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Item$$EnhancerByMockitoWithCGLIB$ffb1868 cannot be returned by getMetaClass()
getMetaClass() should return MetaClass
可以监视 return 模拟对象吗?这个错误的原因是什么?以及如何修复它?
谢谢!
@Mock
不适用于局部变量。为什么你仍然需要模拟?我认为您不需要嘲笑该项目。只需根据您的需要创建一个测试对象 Item
。模拟是为了防止真正的方法被调用。
试试这个:
@Test
public void testCalculate() {
Item item = new Item();
Article article = new Article();
Article articleSpy = Mockito.spy(article);
Mockito.doReturn(item).when(articleSpy).prepareItem();
article.calculate();
}
如果您真的需要间谍 return Mock,Item item = Mockito.mock(Item.class);
应该可以在 Mocktio 1.10 中使用。我不建议您这样做,因为在大多数情况下不需要这样做。大多数时候您可以简化代码(=> 测试驱动开发)。
如果您绝对需要它,这将是一个有效的示例:
Item item = Mockito.mock(Item.class);
Mockito.when(item.getInfo()).thenReturn("bla bla");
Article article = Mockito.spy(Article.class);
Mockito.doReturn(item).when(article).prepareItem();
article.calculate();