- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试模拟一个抽象类,但据我所见,我认为这是不可能的。我们有一些使用泛型的类,它们必须扩展一个特定的抽象类。有一整群人,他们被 mock 成功了。抽象类有一个处理返回泛型的方法,看起来像这样:
public abstract class ChildPresenter <T extends ChildView> {
private T view;
public abstract T getView();
}
我们正在测试的类中包含以下内容:
public class ParentPresenter {
private ConcreteChildPresenter1 childPresenter1;
private ConcreteChildPresenter2 childPresenter2;
private ConcreteChildPresenter3 childPresenter3;
private ConcreteChildPresenter4 childPresenter4;
List<ChildPresenter> childPresenters;
}
在构造函数中,使用 Google Guice 注入(inject)这些类,设置为变量,并添加到子演示者列表中。
被测方法是迭代所有 childPresenters
对象并运行方法 getView()
。
我在测试课上尝试过这种方式:
public class ParentPresenterTest {
private ConcreteChildPresenter1 childPresenter1;
private ConcreteChildPresenter2 childPresenter2;
private ConcreteChildPresenter3 childPresenter3;
private ConcreteChildPresenter4 childPresenter4;
private List<ChildPresenter> childPresenters;
//This is an abstract class
private ChildView childView;
@BeforeTest
public void createInjector() {
Guice.createInjector(...//Creates a fake module and does binding for the variables mentioned earlier
//e.g.
childPresenter1 = mock(ConcreteChildPresenter1.class);
binder.bind(ConcreteChildPresenter1.class).toInstance(childPresenter1);
//e.t.c for other variables
//Same for child view
childView = mock(ChildView.class);
binder.bind(ChildView.class).toInstance(childView);
}
childPresenters = new ArrayList<ChildPresenter>();
childPresenters.add(childPresenter1);
//Add all child presenters
for(ChildPresenter childPresenter : childPresenters) {
when(childPresenter.getView()).thenReturn(childView);
}
}
}
问题发生在 when(childPresenter.getView()).thenReturn(childView);
行,因为 Mockito 提示以下消息:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
ChildView$$EnhancerByMockitoWithCGLIB$$2f6a4bd5
cannot be returned by getView() getView() should return ConcreteChildView1
*** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:
This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.
A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
我能理解,但是模拟每个具体的 ChildView
似乎是一种浪费,而我只想确认模拟的 ChildView
使用以下:
verify(childView, atLeast(childPresenters.size())).getView();
还有其他方法吗?我能否以某种方式使用模拟抽象类来代替具体类?
编辑 下面是如何实现 getView()
方法的具体版本:
public ConcreteChildPresenter1<ConreteChildView1> {
@Override
public ConreteChildView1 getView() {
view.buildView();
return view;
}
}
以及所有 subview 扩展的抽象 ChildView
类:
public abstract ChildView {
public abstract void buildView();
}
最佳答案
由于每个子演示者都返回一个特定类型的 View ,正如您已经了解的那样,您不能将它们替换为抽象类 ChildView 的模拟。
有一种方法可以在运行时获取 ChildView 的具体类型,前提是您提供了正确的实现,如下所述:Get generic type of class at runtime
然后你可以用这种方式初始化演示者的模拟:
for(ChildPresenter childPresenter : childPresenters) {
//this getter returns the needed runtime class
when(childPresenter.getView()).thenReturn(mock(childPresenter.getViewType()));
}
关于java - 使用所述类的抽象版本模拟通用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074881/
我正在阅读 Joshua Bloch 的书 effective java。在“favor composition over inheritance”的第 16 条中,他给出了一个使用 HashSet
我一直在阅读 Adam Dymitruk's git workflow ,这一切都很有道理。 我找不到任何讨论的一件事是修复旧版本中的错误。想象一下带有 7.0、7.1、7.2、7.3、7.4、7.4
我有几个类为“itemWerk”的 div。我想切换它们,以便我可以放大它们并将它们设置回原来的大小和位置。 在here 上,我已经走了很远了.这只是我无法开始工作的第二个功能中的定位。我不太确定我应
我正在尝试创建一个检测 USB 插入的服务。为此,我必须通过 RegisterDeviceNotification(HANDLE, LPVOID, DWORD)< 创建一个 HDEVNOTIFY/.
我是一名优秀的程序员,十分优秀!