Mockito:基于另一个对象的Mock对象 [英] Mockito: Mock object based on another

查看:268
本文介绍了Mockito:基于另一个对象的Mock对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嘲笑的对象,例如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆