Mockito - calling an internal method

I have a class called Availability.javaand two methods.

 public Long getStockLevelStage() {
     //some logic
      getStockLevelLimit();
    }

    public Long getStockLevelLimit() {
      String primaryOnlineArea = classificationFeatureHelper.getFirstFeatureName(productModel, FEATURE_CODE_PRODUCT_ONLINE_AREA_PRIMARY, language);
................
      return new Long();
    }

I am writing unit test class AvailabilityTest.java.

@RunWith(MockitoJUnitRunner.class)
public class AvailabilityTest {
  @InjectMocks
  private Availability availability = new Availability();

  @Test
  public void testGetStockLevelStage() {
    availability.getStockLevelStage();
  }
}

When I call a method availability.getStockLevelStage(), it calls a method getStockLevelLimit(). Is it possible to make fun of a call to an internal method?

In this case, I do not want to be getStockLevelLimit()executed when executed getStockLevelStage().

Please, help.

+5
source share
2 answers

if getStockLevelLimit()it should not be executed during the test, it means that you want to mock the tested class.
Doing this reduces the relevance and authenticity of the behavior tested.

, .
, getStockLevelLimit(), , - .
, getStockLevelLimit(), Availability.

+5

:

@RunWith(MockitoJUnitRunner.class)
public class AvailabilityTest {
    @InjectMocks
    @Spy
    private Availability availability = new Availability();

    @Test
    public void testGetStockLevelStage() {
       Mockito.doReturn(expectedLong).when(availability).getStockLevelLimit();
       availability.getStockLevelStage();
    }
}

, , .

+5

Source: https://habr.com/ru/post/1670436/


All Articles