function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aakash Saluja 7Aakash Saluja 7 

test class not covering

I have an  api class and have to wite test class but test class  is not covering :

class;
public class fileViewController { 
ApexPages.StandardController controller; 
public Boolean httpErrorReceived {get; set;} 
public String respFileString {get; set;} 
public String contentType {get;set;} 

public fileViewController(ApexPages.StandardController controller){ 
this.controller = controller; 
httpErrorReceived = false; 


public void viewFile(){ 
try{ 
Case req = getPortfolioUrl(); 
if(null !=req.Portfolio_Analysis_Report__c && req.View_checkbox_c){ 
String authorizationHeader = 'Bearer ' + PortfolioAnalysisCallouts.getAuthToken(); 

Http http = new Http(); 
HttpRequest request = new HttpRequest(); 
request.setMethod('GET'); 
request.setHeader('Authorization', authorizationHeader); 
request.setEndpoint(req.Portfolio_Analysis_Report__c); 
system.debug('Request==='+request); 

HttpResponse response = http.send(request); 
system.debug('Response==='+response); 
if(response.getStatusCode()==200 || response.getStatusCode()==202){ 
//respFileString = response.getBody(); 
respFileString = EncodingUtil.Base64Encode(response.getBodyAsBlob()); 
contentType = 'application/pdf'; 
}else{ 
respFileString = 'Connection Failed '; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

}else{ 
respFileString = '\"Analysis Report\" is not available'; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

}catch(Exception e){ 
respFileString = 'Connection Failed:"; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

System.debug('respFileString=='+respFileString); 


private Case getPortfolioUrl() { 
return [select 
Id, 
Portfolio_Analysis_Report__c, 
View_checkbox_c 
from Case 
where Id =: ApexPages.currentPage().getParameters().get('id')]; 

private static String getUrl(){ 
URLParameters__c urls = URLParameters__c.getOrgDefaults();//custom setting 
String url = urls.newVal__c; 
return url; 



Mock class:
@isTest 

global class MockHttpResponseGenerator implements HttpCalloutMock { 

global HTTPResponse respond(HTTPRequest req) { 

System.assertEquals('https://example.com', req.getEndpoint()); 
System.assertEquals('GET', req.getMethod()); 


HttpResponse res = new HttpResponse(); 
res.setHeader('Content-Type', 'application/json'); 
res.setBody('{"test":"test1"}'); 
res.setStatusCode(200); 
return res; 



test class: 
@isTest 
public class ViewPortfolioPDFControllerTest { 
public static testMethod void method1() { 


case con = new case(); 
con.Subject = 'Test Subject'; 
con.Initial_Status__c ='Takeovers'; 
con.status='In Progress'; 
con.Total_Assets__c = 0; 
con.Portfolio_Analysis_Report__c= '/services/apexrest/claimApi'; 
con.View_checkbox_c=true; 
insert con; 

Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); 
Test.startTest(); 

apexpages.currentpage().getparameters().put('Cid' , con.Id); 
ApexPages.StandardController sc = new ApexPages.standardController(con); 
ViewPortfolioPDFController vpc= new ViewPortfolioPDFController(sc); 
vpc.viewPortfolioPDF(); 

Test.stopTest(); 
}

can anyone please help
Jay GuruswamyJay Guruswamy
Hi,

I assume you meant, when you run this test class you are getting error.

If so, below is the solution.

if(!Test.isRunningTest()){
   HttpResponse response = http.send(request); 
}


put the http.send line within an if like the one above. That makes the lines to execute only when it is not being tested.

 
Aakash Saluja 7Aakash Saluja 7
Tried the above but it is not entering in the return line of the getPortfolioUrl() method and therefore not entring in the viewFile() method
Jay GuruswamyJay Guruswamy
After vpc.viewPortfolioPDF Assign value for Id and call getPortfolioUrl explicitly.
Aakash Saluja 7Aakash Saluja 7
Ihave done like
ApexPages.currentPage().getParameters().put('id','c2.id');
       vpc.getPortfolioAnalysisURL();
but still nothing is happening. Can you please tell in which you are asking to pass the id.
Jay GuruswamyJay Guruswamy
ApexPages.currentPage().getParameters().put('id',con.Id);
vpc.getPortfolioAnalysisURL();
vpc.getUrl();
Aakash Saluja 7Aakash Saluja 7
Did this but still not covering the soql in the return statement of getPortfolioUrl() method
Jay GuruswamyJay Guruswamy
sorry, I did not notice that you had not called viewFile.

ApexPages.currentPage().getParameters().put('id',con.Id);
vpc.viewFile();
vpc.getUrl();


 
Aakash Saluja 7Aakash Saluja 7
Thanks,SOQL got covered but from Http http = new Http(); it is not getting covered
  
Jay GuruswamyJay Guruswamy
apexpages.currentpage().getparameters().put('Cid' , con.Id); 

change above to

apexpages.currentpage().getparameters().put('id' , con.Id); 
Aakash Saluja 7Aakash Saluja 7
Its this only and SOQL is also working fine but the problem is in this line  Http http = new Http();--It is not getting covered
Jay GuruswamyJay Guruswamy
sorry, can't help any more on this.

put a screen shot of the test coverage, someone will jump in.