CarbonContext is a thread-local variable. That means the carbon context can
be accessible from anywhere within a given thread. So, you may not need to
mock it!

Since the test class and the testing class runs in the same thread, we can
try to populate the CarbonContext in the @Test method before actual testing
class invocations.

Please see the following code example:

@Test
public void testSetClaimValue() throws Exception{
try {
    PrivilegedCarbonContext.startTenantFlow();

PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);

PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);

//actual test code and assertions

} finally {
    PrivilegedCarbonContext.endTenantFlow();
}

}

We of course need to test this out. But I think this is ideal if this works.



On Wed, Oct 4, 2017 at 10:42 PM, Dharshana Warusavitharana <
dharsha...@wso2.com> wrote:

> Hi Sivaramya,
>
> You dont have to mock the whole carbon context here. You can send a mock
> payload when the particular method is called.
>
> Are you using powerMock + mockito if so do as the document sample in [1].
>
> [1]. https://github.com/searls/mockito-testng-example/
> blob/master/presentation/Mockito.pdf
>
> Thank you,
> Dharshana.
>
>
>
> On Wed, Oct 4, 2017 at 9:55 PM, Malaka Silva <mal...@wso2.com> wrote:
>
>> +Dharshana
>>
>> On Wed, Oct 4, 2017 at 8:26 PM, Sivaramya Sivanathan <sivara...@wso2.com>
>> wrote:
>>
>>> Hi all,
>>>
>>> Currently I am working on the unit testing for the esb-connector-jms
>>> extension. For that I need to mock the CarbonContext for the method
>>> CarbonContext.getThreadLocalCarbonContext().getTenantId(). But, I'm
>>> unable mock the CarbonContext.
>>> Can any one suggest me how can we mock the CarbonContext?
>>>
>>> Thanks,
>>> Sivaramya Sivanathan
>>> Associate Software Engineer | WSO2
>>> Tel: 0770874960 <077%20087%204960>
>>> WSO2 Inc : http://wso2.org
>>> <http://www.google.com/url?q=http%3A%2F%2Fwso2.org&sa=D&sntz=1&usg=AFQjCNE_eTDfyl2ibPcq0hcXvRDNVuQmMg>
>>> LinkedIn | www.linkedin.com/in/sivaramya
>>>
>>
>>
>>
>> --
>>
>> Best Regards,
>>
>> Malaka Silva
>> Associate Director / Architect
>> M: +94 777 219 791 <077%20721%209791>
>> Tel : 94 11 214 5345
>> Fax :94 11 2145300 <011%202%20145300>
>> Skype : malaka.sampath.silva
>> LinkedIn : http://www.linkedin.com/pub/malaka-silva/6/33/77
>> Blog : http://mrmalakasilva.blogspot.com/
>>
>> WSO2, Inc.
>> lean . enterprise . middleware
>> https://wso2.com/signature
>> http://www.wso2.com/about/team/malaka-silva/
>> <http://wso2.com/about/team/malaka-silva/>
>> https://store.wso2.com/store/
>>
>> Don't make Trees rare, we should keep them with care
>>
>
>
>
> --
>
> Dharshana Warusavitharana
> Associate Technical Lead
> WSO2 Inc. http://wso2.com
> email : dharsha...@wso2.com <dharsha...@wso2.com>
> Tel  : +94 11 214 5345
> Fax :+94 11 2145300 <011%202%20145300>
> cell : +94770342233 <077%20034%202233>
> blog : http://dharshanaw.blogspot.com
>
> lean . enterprise . middleware
>
> _______________________________________________
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 

*Kasun Gajasinghe*Associate Technical Lead, WSO2 Inc.
email: kasung AT spamfree wso2.com
linked-in: http://lk.linkedin.com/in/gajasinghe
blog: http://kasunbg.org
phone: +1 650-745-4499, 77 678 0813
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to