Hi, What if you write a wrapper class with following constructor. public RESTAPIAdminClientWrapper extends RESTAPIAdminClient{
public RESTAPIAdminClientWrapper(RestApiAdminStub restApiAdminStub) { this.restApiAdminStub = restApiAdminStub; } } Then in the test method you can mock the RestApiAdminStub class and pass it to the constructor. RestApiAdminStub restApiAdminStub = Mockito.mock(RestApiAdminStub).class; RESTAPIAdminClient restAPIAdminClient = new RESTAPIAdminClientWrapper(r estApiAdminStub); Thanks! Rajith On Tue, Oct 10, 2017 at 10:07 AM, Ishara Cooray <isha...@wso2.com> wrote: > Thanks for the reply Fazlan. > > One option is to move the new object creation into a protected method and > override the implementation in a subclass. The con is this will add > additional lines to the class being tested and therefore will have to be > decided whether the effort is useful case by case. > > We can use overriding option if the method is being used in multiple > places. But if it has only one usage it is not wise to got for that option > as it will increase the line count and will have a negative impact in > coverage reports. > > > Thanks & Regards, > Ishara Cooray > Senior Software Engineer > Mobile : +9477 262 9512 <+94%2077%20262%209512> > WSO2, Inc. | http://wso2.com/ > Lean . Enterprise . Middleware > > On Tue, Oct 10, 2017 at 9:57 AM, Fazlan Nazeem <fazl...@wso2.com> wrote: > >> Hi Ishara, >> >> Please check[1] which discusses this issue in Jacoco. >> >> One option is to move the new object creation into a protected method and >> override the implementation in a subclass. The con is this will add >> additional lines to the class being tested and therefore will have to be >> decided whether the effort is useful case by case. >> >> But if the method being tested is static then there is no way to override >> and hence no way to unit test this without a refactoring effort as I am >> aware of. >> >> We have mixed object creation with business logic in the code you have >> provided. This leads to untestable code. >> >> [1] https://github.com/jacoco/jacoco/issues/51 >> >> On Tue, Oct 10, 2017 at 9:31 AM, Ishara Cooray <isha...@wso2.com> wrote: >> >>> 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 <+94%2077%20262%209512> >>> WSO2, Inc. | http://wso2.com/ >>> Lean . Enterprise . Middleware >>> >>> _______________________________________________ >>> Dev mailing list >>> Dev@wso2.org >>> http://wso2.org/cgi-bin/mailman/listinfo/dev >>> >>> >> >> >> -- >> Thanks & Regards, >> >> *Fazlan Nazeem* >> Senior Software Engineer >> WSO2 Inc >> Mobile : +94772338839 >> <%2B94%20%280%29%20773%20451194> >> fazl...@wso2.com >> > > > _______________________________________________ > Dev mailing list > Dev@wso2.org > http://wso2.org/cgi-bin/mailman/listinfo/dev > > -- Rajith Roshan Senior Software Engineer, WSO2 Inc. Mobile: +94-7 <%2B94-71-554-8430>17-064-214
_______________________________________________ Dev mailing list Dev@wso2.org http://wso2.org/cgi-bin/mailman/listinfo/dev