0

Stuck with mockito on some basic stuff that I cannot seem to figure out.

Getting the following 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.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.


    at com.saasbee.webapp.pbx.ControllerTest.negative_test_for_issuer_for_JWT_used_in_method_getCallInNumbersForPBX(ControllerTest.java:29)

for test case:

  @Test
    public void negative_test_for_issuer_for_JWT_used_in_method_getCallInNumbersForPBX() {
        DecodedJWT decodedJWT = Mockito.mock(DecodedJWT.class);
        ConfigService configService = Mockito.mock(ConfigServiceImpl.class);
        try (MockedStatic<JwtUtil> jwtUtil = Mockito.mockStatic(JwtUtil.class))
        {
            Mockito.when(configService.isEnableConfig(configService.ENABLE_CHECK_ISSUER_PBX)).thenReturn(true);
            Mockito.when(WebSplitUtils.getConfigService()).thenReturn(configService);
            Assertions.assertThrows(TokenException.class,()-> JwtUtil.checkIssuer(decodedJWT, Issuer.PBXWEB.value()));
        }
    }

Check Issuer function:

public static void checkIssuer(@NotNull DecodedJWT decodedJwt, String issuer) {
        ConfigService configService = WebSplitUtils.getConfigService();
        if (configService != null) {
            boolean enableCheckIssuerPbx = configService.isEnableConfig(ConfigServiceImpl.ENABLE_CHECK_ISSUER_PBX);
            if (enableCheckIssuerPbx) {
                if (!Objects.equals(issuer, decodedJwt.getIssuer())) {
                    logger.warn("invalid token");
                    throw new TokenException("invalid token");
                }
            }
        }
    }

I want to control the result of these 2 method calls:

ConfigService configService = WebSplitUtils.getConfigService();

&&

if (configService != null) {
    boolean enableCheckIssuerPbx = configService.isEnableConfig(ConfigServiceImpl.ENABLE_CHECK_ISSUER_PBX);

which I thought would be done by these 2 lines but ovs it isn't doing that:

Mockito.when(configService.isEnableConfig(configService.ENABLE_CHECK_ISSUER_PBX)).thenReturn(true);
Mockito.when(WebSplitUtils.getConfigService()).thenReturn(configService);
          

More Info---

public final class WebSplitUtils {
    public static ConfigService getConfigService() {
            if (!SpringUtils.isApplicationContextLoaded()) {
                return null;
            }
            if (configService == null) {
                ConfigService configServiceTmp = SpringUtils.getApplicationContext().getBean(ConfigService.class);
                if (configServiceTmp != null) {
                    synchronized (WebSplitUtils.class) {
                        if (configService == null) {
                            configService = configServiceTmp;
                        }
                    }
                }
            }
            return configService;
        }
}

&&

Verified that mock-maker-inline does exist inside mockito-extensions.

0 Answers0