Mockito:基于另一个对象的Mock对象 [英] Mockito: Mock object based on another
问题描述
我有一个嘲笑的对象,例如Car porsche
.它有4个车轮,1个挡风玻璃,1个发动机,名称为 Porsche .这是通过when()
和thenReturn()
完成的.
I have a mocked object, say Car porsche
. It has 4 wheels, 1 windscreen,1 engine and name Porsche. This is done via when()
and thenReturn()
.
我想创建另一个模拟的Car实例,例如 trabant ,其行为(再次是when()
和thenReturn()
)与 porsche 实例完全一样返回一个不同的名称.
I want to create another mocked Car instance, say trabant, that will behave (again when()
and thenReturn()
) exactly as the porsche instance, only will return a different name.
是否可以在不重复所有when()
和thenReturn()
步骤的情况下基于第一个模型创建第二个模型?
Is it possible to create the 2nd mock based on the 1st one without repeating all the when()
and thenReturn()
steps?
推荐答案
在您的测试中创建一个为给定的模拟对象设置通用期望的私有方法呢?
What about creating a private method in your test that sets the common expectations on a given mock object?
这篇关于Mockito:基于另一个对象的Mock对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!