0

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] );

JavaGen
  • 1
  • 1

1 Answers1

0

For Mockito even though the Predicates have the same implementation they are different instances and it cannot just match these in your case.

You can try to use a wildcard and accept any Predicate:

when(any(Predicate.class)).thenReturn(predicate);

If that is not enough you most likely need to use a custom ArgumentMatcher where you would dynamically check the incoming predicate:

when(argThat(new MyPredicateMatcher())).thenReturn(predicate);
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • Thanks Mackiej . Well i tried Argument matcher too. But no luck. I updated my question, i tried calling an empty dummy method with no arguments. even then i get same error. – JavaGen Jun 07 '19 at 07:22
  • No, i get org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced or misused argument matcher detected . Please see my updated question i am not even to call testdummy method. some problem with flow probably. – JavaGen Jun 07 '19 at 07:35
  • Update: i am able to the method called if use spy instead of mock. Can you pls advise further if use of spy is okay OR else if some change on above code with mock can work. – JavaGen Jun 07 '19 at 11:22
  • You need to use static mocking, spying.. are you sure about your design? Maybe its time to refactor a bit – Maciej Kowalski Jun 07 '19 at 12:19
  • pls advise how to refactor – JavaGen Jun 07 '19 at 14:37