[英]How to (automatically) create a mock object for an interface from an object of a class implementing that interface?
well I have an interface called InputInterface, which has some get methods. 好吧,我有一个名为InputInterface的接口,其中有一些get方法。 I also have a complex gui class, which implements that InputInterface, now since it's too complicated to serialize that whole class and it extends and implements more interfaces, I'd like to create something like a mock object, which would create something like a copy of the answers that my complex class gives out at the current moment.
我也有一个复杂的gui类,它实现了那个InputInterface,因为它太复杂了,无法序列化整个类,并且它扩展并实现了更多接口,所以我想创建一个类似于模拟对象的东西,该对象可以像创建一个副本一样当前我的复杂课堂给出的答案中的一部分。
I wanted to create a class implementing that interface, which would hold the values, but then I realized this should be a standard procedure for things like EasyMock or other mocking libs? 我想创建一个实现该接口的类,该类将保存值,但是后来我意识到这应该是EasyMock或其他模拟库之类的标准过程? Just how can this be done?
请问该怎么做?
Thanks in advance. 提前致谢。
You can actually instantiate an interface, specifying the required methods. 您实际上可以实例化一个接口,指定所需的方法。
MyInterface myInterface = new MyInterface() {
public void requiredMethod(...) {
//...
}
};
Mockito provides a "spy" mechanism to wrap an existing object and thereby be able to observe method calls and potentially stub individual method calls. Mockito提供了一种“间谍”机制来包装现有对象,从而能够观察方法调用并可能存根单个方法调用。 However, it does not provide a mechanism to cache the first call to the spied object and then continuously return this value.
但是,它没有提供一种机制来缓存对间谍对象的第一个调用,然后连续返回此值。
Mockito - see the section of "Spy of real object" Mockito-请参阅“实物间谍”部分
I found this via a google search of "Mockito spy cache". 我通过Google搜索“ Mockito间谍缓存”发现了这一点。 It seems like it might do the trick.
似乎可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.