Friday 12 December 2014

TEST CLASSES IN SALESFORCE


Test.startTest() and Test.stopTest() are very useful when your test class hits Sales force Governor Limits.

The code inside Test.startTest() and Test.stopTest() have new set of Sales force Governor Limits.

As a good practice, make sure initializing the variables, fetching records, creating and updating records are coded before Test.startTest() and Test.stopTest()

and calling the controllers for code coverage is done inside Test.startTest() and Test.stopTest().

The code before Test.startTest() and after Test.stopTest() have new set of Sales force Governor Limits and code between Test.startTest() and Test.stopTest()

have new set of Sales force Governor Limits.


EX:

private class TestClass {
    static testMethod void test() {
        Test.startTest();
        Call the controller for code coverage
        Test.stopTest();
    }
}

Normal Test class:

public class demoTestClass(){ 
public void demoMethod(){
.

.

.

}

}

@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