You need to sign in to do that
Don't have an account?
rupesh ranjan
I want to write Test class
I want to write Test class for belowclass
public class Contactpage{ public Contactpage(ApexPages.StandardController controller) { } public List<contactDemoMailSentwrap> ConsoleWrapperList4{get;set;} public List<contactDemoMailSentwrap> getperformcallout4(){ ConsoleWrapperList4 = new List<contactDemoMailSentwrap>(); HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); req.setEndpoint('https://www.demomail.net/sf/api/ContactDemoMailSent?Email=twilms@verisign.con&NameSpace=Demos%20On%20Demand%20Report'); req.setMethod('GET'); req.setTimeout(10000); res = http.send(req); //System.assert(false,res.getBody()+'-----'); if(res.getstatusCode() == 200 && res.getbody() != null){ String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r',''); ConsoleWrapperList4=(List<contactDemoMailSentwrap>)System.JSON.deserialize(replaceIllegal,List<contactDemoMailSentwrap>.class); } return consolewrapperlist4; } }
For the Above Apex class we need to create the Webservice Mock Class,
Please find the below code for more information,
Test Class
@isTest
public class ContactpageTest
{
static testmethod void Myunittest1()
{
// Your Standard object instance
Standardobject a = new standardobject();
insert a;
Contactpage cp = new Contactpage(new ApexPages.StandardController(a));
Test.setMock(HttpCalloutMock.class, new ContactpageTestmock());
HttpResponse res = Contactpage.getperformcallout4();
String contentType = res.getHeader('Content-Type');
System.assert(contentType == 'application/json');
String actualValue = res.getBody();
String expectedValue = '{"foo":"bar"}';
System.assertEquals(actualValue, expectedValue);
System.assertEquals(200, res.getStatusCode());
cp.getperformcallout4();
}
}
Mock Class
@IsTest
global class ContactpageTestmock implements HttpCalloutMock
{
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('https://www.demomail.net/sf/api/ContactDemoMailSent?Email=twilms@verisign.con&NameSpace=Demos%20On%20Demand%20Report', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"foo":"bar"}');
res.setStatusCode(200);
return res;
}
}
Class------- homepageTestmock Test Class