gpt4 book ai didi

java - 使用所述类的抽象版本模拟通用类

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:10 25 4
gpt4 key购买 nike

我正在尝试模拟一个抽象类,但据我所见,我认为这是不可能的。我们有一些使用泛型的类,它们必须扩展一个特定的抽象类。有一整群人,他们被 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:

  1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.

  2. 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com