Constructor mocking not working inside Enum method

243 views
Skip to first unread message

Sathyakumar Seshachalam

unread,
Jul 23, 2015, 4:59:57 AM7/23/15
to PowerMock
I have been trying to mock a constructor, However this does not seem to work inside an Enum's method, but works very well inside a static method of the same enum. Am using mockito extension.
Below is an example to reproduce the problem I am trying to describe.

public enum AnEnumExample {
    NOT_WORKING {
        @Override
        public String testViolatingMethod() {
            WithNewPreparedClass anObj = new WithNewPreparedClass();
            return anObj.getValue();
        }
    };

    public abstract String testViolatingMethod();
    public static String nonViolatingMethod() {
        WithNewPreparedClass anObj = new WithNewPreparedClass();
        return anObj.getValue();
    }
}

And testng test class to simulate the problem.
@Test
@PrepareForTest(AnEnumExample.class)
public class AnEnumExampleTestCase extends PowerMockTestCase {

    @BeforeMethod
    public void setup() throws Exception {
        WithNewPreparedClass mockObj = mock(WithNewPreparedClass.class);
        when(mockObj.getValue()).thenReturn("mockAnswer");
        PowerMockito.whenNew(WithNewPreparedClass.class).withNoArguments().thenReturn(mockObj);
    }

    public void shouldReturnMockAnswerOnStaticMethod() {
        //This is successful
        assertEquals(AnEnumExample.nonViolatingMethod(), "mockAnswer");
    }

    public void shouldReturnMockAnswerOnEnumMethod() {
        //This is not successful
        assertEquals(AnEnumExample.NOT_WORKING.testViolatingMethod(), "mockAnswer");
    }
}

Does look like a bug. Or Is there a problem with any of my configuration ?

powermock version = 1.6.2
mockito version = 2.0.2-beta

Johan Haleby

unread,
Jul 23, 2015, 5:24:11 AM7/23/15
to powe...@googlegroups.com
Yes this is a bug and it has been reported previously. Please help out with a pull request.

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Sandesh Nayak

unread,
Jul 5, 2018, 9:23:45 AM7/5/18
to PowerMock
Hi
Did you able to resolve this issue..?
Reply all
Reply to author
Forward
0 new messages