Mocking a private method with a service interface and its implementing class

555 views
Skip to first unread message

laredo...@zipmail.com

unread,
Sep 26, 2014, 9:53:31 AM9/26/14
to powe...@googlegroups.com

Hi,

I’m using Mockito 1.9.5, PowerMock 1.5.6, JUnit 4.11, and Spring 3.1.4.RELEASE.  I want to mock a private method, but I’m confused how do it when the declared class of my spied object is actually an interface.  Here is the class with the private method …

    @Service
    public class MyServiceImpl implements MyService
    {
        …
        private void myMethod(byte[] data, UserFile userFile, User user)
        {

And here’s what I’m trying to do in my JUnit test …

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "classpath:test-context.xml" })
    @PrepareForTest(MyService.class)
    public class MyServiceIT
    {

        @Rule
        public PowerMockRule rule = new PowerMockRule();
   
        @Autowired
        private MyService m_mySvc;
        private MyService m_mySvcSpy;
   
        @Before
        public final void setup() throws Exception
        {
            m_mySvcSpy = PowerMockito.spy(m_userFileSvc);
            PowerMockito.doNothing().when(m_mySvcSpy, PowerMockito.method(MyService.class, “myMethod”, byte[].class, UserFile.class, User.class))
                .withArguments(Matchers.any(byte[].class), Matchers.any(UserFile.class), Matchers.any(User.class));
        }   // setup


I get the following exception.  How can I accurately mock the private method?

    testMyMethod(org.mainco.subco.user.service.MyServiceIT)  Time elapsed: 46.084 sec  <<< ERROR!
org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name ‘myMethod’ with parameter types: [ [B, org.mainco.subco.user.domain.UserFile, org.mainco.subco.user.domain.User ] in class org.mainco.subco.user.service.MyService.
        at org.powermock.reflect.internal.WhiteboxImpl.throwExceptionIfMethodWasNotFound(WhiteboxImpl.java:1167)
        at org.powermock.api.support.membermodification.MemberMatcher.method(MemberMatcher.java:82)
        at org.mainco.subco.user.service.MyServiceIT.setup(MyServiceIT.java:52)

Johan Haleby

unread,
Sep 30, 2014, 1:52:27 PM9/30/14
to powe...@googlegroups.com
I would rather just suppress the method. Something like:

suppress(method(MyService.class,  “myMethod”));

See documentation here.

/Johan

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

Reply all
Reply to author
Forward
0 new messages