[英]How can I mock an instance of an enum class with PowerMock & Mockito?
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
.我需要一个enum class
的简单模拟实例。 I don't need to mock any of its methods.我不需要嘲笑它的任何方法。
Here is the class I want to mock:这是我想要模拟的课程:
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;
}
}
I have the following TestNG setup:我有以下 TestNG 设置:
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);
}
}
You need to run this with PowerMockRunner您需要使用 PowerMockRunner 运行它
eg.例如。
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ExampleEnumerable.class})
@Test(groups = {"LoadableBuilderTestGroup"})
public class LoadableBuilderTest {
private ExampleEnumerable mockEnumerable;
@BeforeMethod
public void setUp() {
mockEnumerable = mock(ExampleEnumerable.class);
}
}
I got this working by extending the PowerMockTestCase class that handles this kind of thing for TestNG:我通过扩展为 TestNG 处理此类事情的 PowerMockTestCase 类来实现此目的:
@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);
}
}
Hi guys, to me worked as below:嗨,伙计们,对我来说,工作如下:
First add power mockito on pom.xml:首先在 pom.xml 上添加 power mockito:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
and then on my class:然后在我的课上:
@RunWith(PowerMockRunner.class)
@PrepareForTest({EnumClass.class})
public class EnumClassTest{
private EnumClassTest enumClassTest;
@Before
public void setUp() {
enumClassTest = mock(EnumClassTest.class);
}
@Test
public void someMethod() {
//My code test here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.