gpt4 book ai didi

java - 用于泛型类扩展的 Mockito/Powermock 类构造

转载 作者:行者123 更新时间:2023-12-02 02:34:16 25 4
gpt4 key购买 nike

不确定我的问题措辞是否正确,但我正在尝试模拟作为泛型传递到我想要测试的对象中的类的构造。下面是我正在测试的对象中发生的情况的示例:

MyClass(boolean historic, Class<? extends Learner> learner) {
this.learner = learner.newInstance();
this.historic = historic;
}

学习者是我想要模拟的,它是我构建的各种学习者类(class)的接口(interface)。我不想在这里测试他们的逻辑,这就是为什么我想 mock 他们并控制他们返回的内容。我正在尝试使用以下设置(静态学习器类没有构造函数参数):

@Test
@PrepareForTest({MyClass.class, Learner.class, StaticLearner.class})
public void test() {
Learner mockLearner = Mockito.mock(StaticLearner.class);
PowerMockito.whenNew(Learner.class)
.withNoArguments()
.thenReturn(mockLearner);

MyClass myClass = new MyClass(true, StaticLearner.class);
myClass.process();
}

问题是 Powermock 无法构造学习器,因为它说找不到构造函数。这是有道理的,因为 Learner 类只是一个接口(interface)。那么,当 StaticLearner 只是从 Learner 继承的泛型时,我该如何模拟传入和构造的 StaticLearner 呢?

这是我收到的错误:

org.powermock.reflect.exceptions.ConstructorNotFoundException: 
No constructor found in class 'com.myco.processing.learners.Learner' with parameter types: [ <none> ].

最佳答案

PowerMockito.whenNew(Learner.class) 替换为 PowerMockito.whenNew(StaticLearner.class)。例如,

    @Test
@PrepareForTest({MyClass.class})
public void test() throws Exception {
StaticLearner mockLearner = Mockito.mock(StaticLearner.class);

PowerMockito.whenNew(StaticLearner.class)
.withNoArguments()
.thenReturn(mockLearner);

MyClass myClass = new MyClass(true, StaticLearner.class);
myClass.process();
}

更新

PowerMockito.whenNew 在使用 new 关键字时起作用,例如 new StaticLearner()。如果您使用StaticLearner.class.newInstance(),它将不起作用。

  • 如果您想模拟 MyClass,请将 Learner 对象的创建委托(delegate)给新工厂类 LearnerFactory

    public class LearnerFactory {

    public static Learner getInstance(
    Class<? extends Learner> learner) throws IllegalAccessException, InstantiationException {

    return learner.newInstance();
    }
    }

    public class MyClass {

    private boolean historic;

    private Learner learner;

    public MyClass(boolean historic,
    Class<? extends Learner> learner) throws IllegalAccessException, InstantiationException {
    this.learner = LearnerFactory.getInstance(learner);
    this.historic = historic;
    }

    public void process() {
    ...
    }
    }
  • 现在模拟工厂类以返回模拟StaticLearner

     @RunWith(PowerMockRunner.class)
    @PrepareForTest({MyClass.class, LearnerFactory.class})
    public class MyClassTest {

    @Test
    public void test2() throws Exception {
    StaticLearner mockLearner =
    PowerMockito.mock(StaticLearner.class);
    //if needed
    when(mockLearner.doSomething(anyString()))
    .thenReturn("dummy");

    PowerMockito.mockStatic(LearnerFactory.class);
    when(LearnerFactory.getInstance(eq(StaticLearner.class)))
    .thenReturn(mockLearner);

    MyClass myClass = new MyClass(true, StaticLearner.class);
    myClass.process();
    }

    }

关于java - 用于泛型类扩展的 Mockito/Powermock 类构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634070/

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