Question

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!

Was it helpful?

Solution

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!.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top