如何使用 PowerMock & 模拟枚举类的实例莫基托?

How can I mock an instance of an enum class with PowerMock amp; Mockito?(如何使用 PowerMock amp; 模拟枚举类的实例莫基托?)
本文介绍了如何使用 PowerMock & 模拟枚举类的实例莫基托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我尝试按照这个非常相似的问题的答案中提供的示例进行操作,但它对我不起作用.我收到以下错误消息:

I tried to follow the example offered in the answer to this very similar question, but it does not work for me. I get the following error message:

java.lang.IllegalArgumentException: Cannot subclass final class class com.myproject.test.support.ExampleEnumerable
    at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
    at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
    at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
    at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
    at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)

我需要一个 枚举类 的简单模拟实例.我不需要模拟它的任何方法.

I need a simple mock instance of an enum class. I don't need to mock any of its methods.

这是我要模拟的课程:

public enum ExampleEnumerable implements IEnumerable<ExampleEnumerable> {
    EXAMPLE_ENUM_1("Test Enum 1"),
    EXAMPLE_ENUM_2("Test Enum 2");

    final String alias;

    ExampleEnumerable(final String alias) {
        this.alias = alias;
    }

    @SuppressWarnings({"VariableArgumentMethod", "unchecked"})
    @Override
    public @Nullable
    String getAlias(final @Nonnull IEnumerable<? extends Enum<?>>... context) {
        return alias;
    }
}

我有以下 TestNG 设置:

I have the following TestNG setup:

import static org.powermock.api.mockito.PowerMockito.mock;

@PrepareForTest({ ExampleEnumerable.class})
@Test(groups = {"LoadableBuilderTestGroup"})
public class LoadableBuilderTest {

    private ExampleEnumerable mockEnumerable;

    @BeforeMethod
    public void setUp() {
        mockEnumerable = mock(ExampleEnumerable.class);
    }
}

推荐答案

我通过扩展 PowerMockTestCase 类来为 TestNG 处理这种事情:

I got this working by extending the PowerMockTestCase class that handles this kind of thing for TestNG:

@PrepareForTest(TestEnumerable.class)
@Test(groups = {"LoadableBuilderTestGroup"})
public class LoadableBuilderTest extends PowerMockTestCase {

 private TestEnumerable mockEnumerable;

 @SuppressWarnings("unchecked")
    @BeforeMethod
    public void setUp() {
        mockEnumerable = PowerMockito.mock(TestEnumerable.class);

    }
}

这篇关于如何使用 PowerMock &amp; 模拟枚举类的实例莫基托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)