Since this is a unit test you have no option but to use power mock in order to mock the PrivilegedCarbonContext.endTenantFlow() static function using PowerMockito.mockStatic. I think we must be doing this already, not sure why the test is suddenly failing. We have done this in APIProviderImplTest.
If you dont want to use power mock you will need to refactor the original code to prevent calling the static function directly. On Mon, 7 Jan 2019 at 13:16, Ishara Cooray <[email protected]> wrote: > Hi, > I am running into below error [1] while using below code snippet. > >> AuthorizationGrantCacheEntry cacheEntry = >> AuthorizationGrantCache.getInstance().getValueFromCacheByToken(cacheKey); >> if (cacheEntry == null) { >> return new HashMap<String, String>(); >> } >> Map<ClaimMapping, String> userAttributes = cacheEntry.getUserAttributes(); >> Map<String, String> userClaims = new HashMap<String, String>(); >> for (Map.Entry<ClaimMapping, String> entry : userAttributes.entrySet()) { >> userClaims.put(entry.getKey().getRemoteClaim().getClaimUri(), >> entry.getValue()); >> } >> >> > Because It fails to access PrivilegedCarbonContext at > > * PrivilegedCarbonContext.endTenantFlow();* > This looks to me a concurrency access issue. *Does anyone has an idea of > fixing this without using powermock?* > If i use power mock , *it also fails Prepare classes for test at * > @PrepareForTest() > > PrivilegedCarbonContext.endTenantFlow(); > > > public V getValueFromCache(K key) { >> if (!isEnabled()) { >> return null; >> } >> >> if(key == null) { >> return null; >> } >> >> try { >> PrivilegedCarbonContext.startTenantFlow(); >> PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext >> .getThreadLocalCarbonContext(); >> carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID); >> >> carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); >> Cache<K, V> cache = getBaseCache(); >> if (cache != null && cache.get(key) != null) { >> return (V) cache.get(key); >> } >> return null; >> } finally { >> PrivilegedCarbonContext.endTenantFlow(); >> } >> } >> >> Need to fix the test case without using powermock. > > > [1] > testAbstractJWTGenerator(org.wso2.carbon.apimgt.keymgt.token.TokenGenTest) > Time elapsed: 0 sec <<< ERROR! > java.lang.NoClassDefFoundError: Could not initialize class > org.wso2.carbon.context.PrivilegedCarbonContext > at > org.wso2.carbon.identity.application.common.cache.BaseCache.getValueFromCache(BaseCache.java:158) > at > org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCache.getValueFromCacheByToken(AuthorizationGrantCache.java:86) > at > org.wso2.carbon.apimgt.keymgt.token.JWTGenerator.getClaimsFromCache(JWTGenerator.java:129) > at > org.wso2.carbon.apimgt.keymgt.token.JWTGenerator.populateCustomClaims(JWTGenerator.java:98) > at > org.wso2.carbon.apimgt.keymgt.token.AbstractJWTGenerator.buildBody(AbstractJWTGenerator.java:190) > at > org.wso2.carbon.apimgt.keymgt.token.AbstractJWTGenerator.generateToken(AbstractJWTGenerator.java:143) > at > org.wso2.carbon.apimgt.keymgt.token.TokenGenTest.testAbstractJWTGenerator(TokenGenTest.java:81) > > Thanks & Regards, > Ishara Cooray > Senior Software Engineer > Mobile : +9477 262 9512 > WSO2, Inc. | http://wso2.com/ > Lean . Enterprise . Middleware > -- Regards, Uvindra Mobile: 777733962
_______________________________________________ Dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/dev
