You need to sign in to do that
Don't have an account?
Kishan Kumar 77
This my custom controller which is used for searching opportunity and sending the data using Http REST Callout. I need to write test class for both the methods. Can someone please help me. Thanks in advance.
public with sharing class OppSearch {
public List<Opportunity> OppList { get; set; }
public String searchText { get; set; }
public static Id selectedRec {get;set;}
public integer totalRecs = 0;
public integer OffsetSize = 0;
public integer LimitSize= 5;
public OppSearch()
{
OppList = new List<Opportunity>();
}
public PageReference searchOpportunity()
{
OppList = new List<Opportunity>();
if(String.isBlank(searchText))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter some value'));
}
if(String.isNotBlank(searchText))
{
if(searchText.isNumeric())
{
Decimal searchstringDouble = decimal.valueOf(searchText);
OppList = [select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Amount=:searchstringDouble LIMIT :LimitSize OFFSET :OffsetSize];
totalRecs = OppList.size();
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
else {
String newSearchText = searchText+'%';
OppList =[select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Name like:newSearchText or Account.Name like:newSearchText
or StageName like:newSearchText or Type like:newSearchText LIMIT :LimitSize OFFSET :OffsetSize];
totalRecs = OppList.size();
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
}
return null;
}
public pageReference FirstPage()
{
OffsetSize = 0;
searchOpportunity();
return null;
}
public pageReference previous()
{
OffsetSize = OffsetSize - LimitSize;
searchOpportunity();
return null;
}
public pageReference next()
{
OffsetSize = OffsetSize + LimitSize;
searchOpportunity();
return null;
}
public pageReference LastPage()
{
OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
searchOpportunity();
return null;
}
public boolean getprev()
{
if(OffsetSize == 0)
return true;
else
return false;
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs)
return true;
else
return false;
}
public static HttpResponse sendData()
{
Opportunity op=[select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Id = :selectedRec];
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('Name ', op.Name);
gen.writeStringField('Account',op.Account.Name);
gen.writeStringField('Stage',op.StageName);
gen.writeStringField('Type',op.Type);
gen.writeNumberField('Amount',op.Amount);
gen.writeDateField('Close Date',op.CloseDate);
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
// Sending the http body with JSON
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS);
HttpResponse response = http.send(request);
if (response.getStatusCode() != 201)
{
Opportunity o= [select Integration_Comments__c,Integration_Status__c from Opportunity
where Id =:selectedRec];
o.Integration_Status__c='Not Successful';
o.Integration_Comments__c=response.getStatus();
update o;
System.debug(response.getStatusCode());
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Opps! your Callout was not successful'));
}
else {
Opportunity o= [select Integration_Comments__c,Integration_Status__c from Opportunity
where Id =:selectedRec];
o.Integration_Status__c='Success';
o.Integration_Comments__c='Success';
update o;
System.debug(response.getStatusCode());
}
return response;
}
}
public List<Opportunity> OppList { get; set; }
public String searchText { get; set; }
public static Id selectedRec {get;set;}
public integer totalRecs = 0;
public integer OffsetSize = 0;
public integer LimitSize= 5;
public OppSearch()
{
OppList = new List<Opportunity>();
}
public PageReference searchOpportunity()
{
OppList = new List<Opportunity>();
if(String.isBlank(searchText))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter some value'));
}
if(String.isNotBlank(searchText))
{
if(searchText.isNumeric())
{
Decimal searchstringDouble = decimal.valueOf(searchText);
OppList = [select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Amount=:searchstringDouble LIMIT :LimitSize OFFSET :OffsetSize];
totalRecs = OppList.size();
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
else {
String newSearchText = searchText+'%';
OppList =[select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Name like:newSearchText or Account.Name like:newSearchText
or StageName like:newSearchText or Type like:newSearchText LIMIT :LimitSize OFFSET :OffsetSize];
totalRecs = OppList.size();
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
}
return null;
}
public pageReference FirstPage()
{
OffsetSize = 0;
searchOpportunity();
return null;
}
public pageReference previous()
{
OffsetSize = OffsetSize - LimitSize;
searchOpportunity();
return null;
}
public pageReference next()
{
OffsetSize = OffsetSize + LimitSize;
searchOpportunity();
return null;
}
public pageReference LastPage()
{
OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
searchOpportunity();
return null;
}
public boolean getprev()
{
if(OffsetSize == 0)
return true;
else
return false;
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs)
return true;
else
return false;
}
public static HttpResponse sendData()
{
Opportunity op=[select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Id = :selectedRec];
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('Name ', op.Name);
gen.writeStringField('Account',op.Account.Name);
gen.writeStringField('Stage',op.StageName);
gen.writeStringField('Type',op.Type);
gen.writeNumberField('Amount',op.Amount);
gen.writeDateField('Close Date',op.CloseDate);
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
// Sending the http body with JSON
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS);
HttpResponse response = http.send(request);
if (response.getStatusCode() != 201)
{
Opportunity o= [select Integration_Comments__c,Integration_Status__c from Opportunity
where Id =:selectedRec];
o.Integration_Status__c='Not Successful';
o.Integration_Comments__c=response.getStatus();
update o;
System.debug(response.getStatusCode());
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Opps! your Callout was not successful'));
}
else {
Opportunity o= [select Integration_Comments__c,Integration_Status__c from Opportunity
where Id =:selectedRec];
o.Integration_Status__c='Success';
o.Integration_Comments__c='Success';
update o;
System.debug(response.getStatusCode());
}
return response;
}
}
1. First to Cover the HTTP callouts you must have to create new HTTP Mock Callout Class :
Copy the below code and create the new class in your org:
2. Once the above class created use the below update test class for controller:
I know you will get good coverage based on above code but try to include assert statements in each method to verify the test logic are working as expected.
Thanks,
Maharajan.C
All Answers
1. First to Cover the HTTP callouts you must have to create new HTTP Mock Callout Class :
Copy the below code and create the new class in your org:
2. Once the above class created use the below update test class for controller:
I know you will get good coverage based on above code but try to include assert statements in each method to verify the test logic are working as expected.
Thanks,
Maharajan.C