I know this is typically a bad practice but in my case it is necessary.
I have a case where an Enum holds a class to gain some information. So that Enum creates an instance of that calss in its Constructor.
public enum MyEnum {
CONSTANT(new MyImpl());
private final MyImpl myImpl;
private MyEnum(final MyImpl impl) {
this.myImpl = impl;
}
public void sayHello() {
System.out.println(this.myImpl.getSomethingToSay());
}
}
MyImpl.java
is just a class with a single method that returns a String.
public class MyImpl {
public String getSomethingToSay() {
return "Hello!";
}
}
Now finally the unit test:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
@RunWith(MockitoJUnitRunner.class)
@PrepareForTest({ MyImpl.class, MyEnum.class })
public class MyEnumTest extends PowerMockTestCase {
@Test
public void testSmth2() throws Exception {
MyImpl impl = Mockito.mock(MyImpl.class);
Mockito.when(impl.getSomethingToSay()).thenReturn("It works!");
PowerMockito.whenNew(MyImpl.class).withAnyArguments().thenReturn(impl);
System.out.println(impl.getSomethingToSay());
System.out.println(new MyImpl().getSomethingToSay());
MyEnum.CONSTANT.sayHello();
}
}
The output is:
It works!
Hello!
Hello!
But should be 3 times it works!
Instance mocking means that a statement like: $obj = new \MyNamespace\Foo; …will actually generate a mock object. This is done by replacing the real class with an instance mock (similar to an alias mock), as with mocking public methods.
We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.
While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.
We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.
I found the faulty part.
I changed
@RunWith(MockitoJUnitRunner.class)
to
@RunWith(PowerMockRunner.class)
Now the mocking works. But i have to say that as Jon Skeet printed out, the enum does not have everywhere that mocked member-instance. So in another Unit test calling MyEnum.CONSTANT.sayHello();
will print again it works
instead of Hello!
.
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