I am trying to test a utility class having static methods , most of them static and returning Optional. Lot of other objects and parameter are being passed as parameters which i have mocked using Mockito. I am using PowerMock to call static methods. The problem is when i use verify after actual call of method i want to test i.e MyUtil.getJob(JobManager, "_TEST_dummy_JOBGROUP", Optional.of("CWB-4"));
It is not calling another method which it is supposed to call. i.e.
JobUtil.testDummy();;
Error on console pasted below
i tried mocking predicate and also giving real predicate as you i commented the first line in my test. I tried calling when..then and also commenting it and also calling real method . But no success. Pls ignore any typos in code as i just created dummy .
MyUtilTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyUtil.class)
public class MyUtilTest {
@Mock
JobManager JobManager;
@Mock
Job Job;
@Mock
JobMetadata JobMetadata;
@Mock
private Predicate<Map.Entry<Job, JobMetadata>> predicate;
@InjectMocks
MyUtil MyUtil = new MyUtil();
private ArgumentCaptor<Job> JobArgument = ArgumentCaptor.forClass(Job.class);
@Test
public void testGetJobGroupPredicate() {
//Predicate<Map.Entry<Job, JobMetadata>> jobGroupMatcher = MyUtil.getJobGroupPredicate(jobMeta -> eq("TEST_dummy_JOBGROUP").startsWith(jobMeta));
PowerMockito.mockStatic(MyUtil.class);
when(MyUtil.getJobGroupPredicate(j->"_TEST_dummy_JOBGROUP".startsWith(j))).thenReturn(predicate);
when(JobUtil.testDummy()).thenReturn("called");
try {
MyUtil.getJob(JobManager, "_TEST_dummy_JOBGROUP", Optional.of("TEST-4"));
}
catch(Exception e){}
PowerMockito.verifyStatic(MyUtil.class);
MyUtil.testDummy();;
}
MyUtil.java
public class MyUtil {
public static Optional<Job> getJob(final JobManager jobManager,
final String jobGroup,
Optional<String> feature) throws PlatformClientException {`
String test=MyUtil.testDummy();
Predicate<Map.Entry<Job, JobMetadata>> jobGroupMatcher = getJobGroupPredicate(jobMeta -> jobGroup.startsWith(
jobMeta));
return getWithPredicate(jobManager, jobGroupMatcher, feature);
}
static String testDummy() {
return "helloooooo";
}
}
Wanted but not invoked de.dummy.cloud.wh.jobs.utils.MyUtil.testDummy();
However, there was exactly 1 interaction with this mockde.dummy.cloud.wh.jobs.utils.MyUtil.getJob( Mock for JobManager, hashCode: 1871312485, "_TEST_dummy_JOBGROUP", Optional[TEST-4] ); . Wanted but not invoked de.dummy.cloud.wh.jobs.utils.MyUtil.testDummy();
However, there was exactly 1 interaction with this mockde.dummy.cloud.workbench.jobs.utils.MyUtil.getJob( Mock for JobManager, hashCode: 1871312485, "_TEST_dummy_JOBGROUP", Optional[TEST-4] );