Hi,
Let's say in a legacy code we have new instance created inside the
constructor and it is being used inside methods to be unit tested as below.
public boolean updateApi(String apiConfig, String tenantDomain) throws
AxisFault {
> try {
> return restApiAdminStub.updateApiForTenant(qualifiedName, apiConfig,
> tenantDomain);
> } catch (Exception e) {
> throw new AxisFault("Error while updating API in the gateway. " +
> e.getMessage(), e);
> }
> }
>
>
Where restApiAdminStub is created within the constructor as below.
> public RESTAPIAdminClient(String apiProviderName, String apiName, String
> version) throws AxisFault {
>> this.qualifiedName = apiProviderName + "--" + apiName + ":v" + version;
>> this.qualifiedDefaultApiName = apiProviderName + "--" + apiName;
>> restApiAdminStub = new RestApiAdminStub(null, backendURLl +
>> "RestApiAdmin");
>>
>> }
>>
>>
One option to mock the new instance is, use PowerMockito.whenNew()
But if we use that, we need to annotate the class that is creating the new
instance with @PrepareForTest annotation and that leads to remove the
@PrepareForTest annotation that we use to annotate Test class. Because of
that this test class is getting excluded from jacoco report.
Do we have any other solution for this?
Thanks & Regards,
Ishara Cooray
Senior Software Engineer
Mobile : +9477 262 9512
WSO2, Inc. | http://wso2.com/
Lean . Enterprise . Middleware
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev