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)