Mockito when().thenReturn calls the method unnecessarily

后端 未结 5 2342
挽巷
挽巷 2021-02-19 02:23

I\'m working a bit on an inherited code. I\'ve written a test that is supposed to catch NullPointerException (for it is trying to call a method from null object)



        
相关标签:
5条回答
  • 2021-02-19 02:42

    I ran across this thread while trying to fix the same issue in my tests.

    In case others see this issue and end up here...In my case it was caused by not using the @PrepareForTest annotation for the support class.

    0 讨论(0)
  • 2021-02-19 02:46

    This may help others who use annotations. If you are using annotations, may be you need to use @Mock instead of @InjectMocks. Because @InjectMocks works as @Spy and @Mock together. And @Spy keeps track of recently executed methods and you may feel that incorrect data is returned/subbed. Check these two:

    https://groups.google.com/forum/?fromgroups#!topic/mockito/9WUvkhZUy90

    http://code.google.com/p/mockito/issues/detail?id=127

    0 讨论(0)
  • 2021-02-19 02:48

    Mockito cannot mock static method. Your when check is not valid:

      when(BasketHelper.getAction(request)).thenReturn(0);
      when(BasketHelper.getActionProduct(site, request)).thenReturn(product);
    

    That is another reason why we want to reduce the use of static method as it is hard to mock.

    There is no easier way to mock the behavior if your class stays like this. However if you want to change your design and make both methods non-static. The correct way of using "when" is to apply the check on mocked object. For example:

      BasketHelper basketHelper = mock(BasketHelper.class);
      when(basketHelper.getAction(request)).thenReturn(0);
      when(basketHelper.getActionProduct(site, request)).thenReturn(product);
    

    But once again, this only work if you re-designed your class's getAction and getProduct method to be NON-STATIC.

    I remember there are some other testing framework that does support mocking static method.

    0 讨论(0)
  • 2021-02-19 03:02

    You can use PowerMock. First create mock of the class on which you are calling static methods -

    mockStatic(BasketHelper.class);
    

    Then define your stubs -

    when(BasketHelper.getAction(request)).thenReturn(0);
    when(BasketHelper.getActionProduct(site, request)).thenReturn(product);
    
    0 讨论(0)
  • 2021-02-19 03:04

    Although it is not good coding practice to mock static method, private method or constructor. But if have some legacy code and you need to mock it. then in that case one can use Powermock with mockito.

    Steps to follow when mocking static method using powermock.
    1) use specific Runner i.e. PowerMockRunner.class.
    2) use annotation @PrepareForTest(UtilityClass.class).
    3) initialise utility class containing static method.
    4) mock static method you want.

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(Utility.class)
    public class mockingStaticMethod(){
    
    @Mock
    Dependency dependency;
    
    @InjectMocks
    SystemUnderTest systemUnderTest;
    
    @Test
    public void testStaticMethod(){
    
        PowerMockito.mockStatic(Utility.class);
        When(Utility.staticMethod(arguments)).thenReturn(expectedValue);
        //systemUnderTest class uses the static method present in Utility class 
        //within the methodCallingStaticMethod()
        result = systemUnderTest.methodCallingStaticMethod();
        assertEquals(expectedValue, actualValue);
    
    
       }
    }
    
    0 讨论(0)
提交回复
热议问题