.
.
.
}
}
@isTest
public class TestMethodClass {
static testMethod void testMethod() {
demoTestClass newDemo = demoTestClass();
newDemo.demoMethod();
}
}
Test Class for Callout :
To cover callout in class we have to write a test class first which is used for dummy callout and that test class has to use in our original test class
so total we have to write 2 test classes
1. To call dummy web service
2. Real test class
Class :
-----
public class CalloutClass {
public static HttpResponse getInfoFromExternalService() {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://graph.facebook.com/oauth/access_token');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
return res;
}
}
Test class for dummy web service :
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
System.assertEquals('https://graph.facebook.com/oauth/access_token', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"foo":"bar"}');
res.setStatusCode(200);
return res;
}
}
Test class for CalloutClass class:
@isTest
private class CalloutClassTest {
@isTest static void testCallout() {
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
HttpResponse res = CalloutClass.getInfoFromExternalService();
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());
}
}
Test class for PageReffernce :
Class:
-----
public PageReference add() {
insert technology;
return null;
}
Test Method:
-----------
public static testMethod void testMyController() {
PageReference pageRef = Page.yourPageName;
Test.setCurrentPage(pageRef);
MyController controller = new MyController();
controller.add();
}
Test Class for batch apex:
@isTest
private class myTestClass {
static testMethod void myTestMethod() {
generateTestData();
Test.startTest();
DemoBatchClass batchObj = new DemoBatchClass();
batchObj.execute();
Test.stopTest();
}
}
No comments:
Post a Comment