Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito exception: Missing method invocation while trying to test an if conditon

I am trying to test for an if condition using when(),thenReturn(), but when I run my test case it gives me missing method invocation even though I have mocked the class where the method is implemented.

this is the if condition I am trying to mock

     if(request.getProcessType() == IPRequest.IPREQUEST_TYPE_TOMO_RECON)//IPREQUEST_TYPE_TOMO_RECON=9, this is the condition I am trying to test    
      {
        params.setTubeAngle(accessor); //I am verifying if these methods are invoked
        params.setTomoFocalSpot(accessor);
      } 

This is how I am checking for the if condition

when(request.getProcessType()).thenReturn(IPRequest.IPREQUEST_TYPE_TOMO_RECON);
        Mockito.verify(ipImgParam,Mockito.times(3)).setTubeAngle(Mockito.any(AttributeExtractor.class));

I have already mocked "request" using @Mock annotation but still getting the below exception.

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.

    at  common.systemreprocessingservice.test.ImageParamsBuilderTest.testbuildProcessingInfoIf(ImageParamsBuilderTest.java:134)

I am not sure what I am doing doing wrong. Can anyone help?

like image 995
hushie Avatar asked Jun 01 '26 10:06

hushie


2 Answers

I cannot write a comment, that's why asking here. What kind of object is "request"? If it is an object of a final class, then you'll need PowerMockito for mocking that.

Also, after using @Mock annotation, did you call MockitoAnnotations.initMocks(testClass.class) method before running the test case?

The following link tells different ways in which you can mock your objects. https://blog.frankel.ch/initializing-your-mockito-mocks/#gsc.tab=0

like image 103
blu3 Avatar answered Jun 02 '26 22:06

blu3


   imageParamsBuilder.buildProcessingInfo(request, info);
    Mockito.verify(ipImgParam, Mockito.times(3)).fillYourSelf(Mockito.any(AttributeExtractor.class));
   when(request.getProcessType()).thenReturn(IPRequest.IPREQUEST_TYPE_TOMO_RECON);
    Mockito.verify(ipImgParam,Mockito.times(3)).setTubeAngle(Mockito.any(AttributeExtractor.class));

The third line here is "too late". You must configure the mock before you call the CuT.


It still doesnt solve my problem

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.

in Test:

when(TagAccessorFactory.getInstance()).thenReturn(tagAccessorFactoryMock);

You can't mock static methods this way, you have to use PowerMockitos when() method.

But (once more) I consider the use of PowerMockito as a surrender to bad design. You should not use static access to get dependencies but pass them in into your class using DI (either manually or preferable using a DI framework).


when I call the

when(request.getProcessType()).thenReturn(IPRequest.IPREQUES‌​‌​T_TYPE_TOMO_RECON)‌​; 

before the method call I am getting exception like this

Wanted but not invoked:" 

you try to test both execution path with the same test method.

You should have separate test methods

like image 35
Timothy Truckle Avatar answered Jun 03 '26 00:06

Timothy Truckle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!