Java 8
Maven 3.9.
I want to verify is void private method call. I try PowerMock.
pom.xml
<properties> <powermock.version>2.0.2</powermock.version> </properties> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> <version>4.4.0</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <scope>test</scope> <version>4.4.0</version> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> Here my service class:
public class MyService { private String data; public MyService(String data) { this.data = data; } public void processData() { // Some processing logic String message = "Processing data: " + data; logEvent(message); // More processing logic } private void logEvent(String message) { // In a real scenario, this would log the event System.out.println("Logging: " + message); } } Here my unit test using PowerMock:
import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import static org.mockito.Mockito.times; import static org.powermock.api.mockito.PowerMockito.verifyPrivate; @RunWith(PowerMockRunner.class) @PrepareForTest(MyService.class) // Prepare MyService for bytecode manipulation public class MyServiceTest { @Test public void testProcessData_shouldCallLogEvent() throws Exception { // Arrange String testData = "sampleData"; MyService service = new MyService(testData); MyService spyService = PowerMockito.spy(service); // Create a spy to track private method calls // Act spyService.processData(); // HERE ERROR!!! // Assert // Verify that the private method 'logEvent' was called exactly once verifyPrivate(spyService, times(1)).invoke("logEvent", "Processing data: sampleData"); } } When I run test testProcessData_shouldCallLogEvent the error is rise:
java.lang.NoSuchMethodError: org.mockito.invocation.InvocationFactory.createInvocation(Ljava/lang/Object;Lorg/mockito/mock/MockCreationSettings;Ljava/lang/reflect/Method;Ljava/util/concurrent/Callable;[Ljava/lang/Object;)Lorg/mockito/invocation/Invocation; at com.myproject.handler.MyService.processData(MyService.java:15) at com.myproject.handler.MyService.processData(MyService.java:14) at com.myproject.handler.MyServiceTest.testProcessData_shouldCallLogEvent(MyServiceTest.java:26) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68) at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)