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.
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:
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);
}
}
Mockito 2 supports mocking of enum, but it is experimental feature. It needs to be enabled explicitly.
Of course you can – and probably will – use Mockito and PowerMock in the same JUnit test at some point of time.
You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once. Therefore, the constructor must be declared private or default. To returns the values of the constants using an instance method(getter).
You need to run this with 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:
@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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With