6

Below is my apex class and trigger, that is used to send sms from 3rd party application and that is working fine, but i'm not getting test coverage for class.

If i use @future method my apex class is working fine, but test coverage is 0%(I have used http mock in test class)

If I use http response I'm not getting SMS, but Test coverage is 100%

public class sendsms2 {
  // @Future(callout=true)
    public static HttpResponse sendsms1(string mobile,string name) {

   // list<account> acc=[SELECT Id,Phone FROM Account where id=:conId];

                  String Username ='abcd.com';
                  String Password = 'xxxx';
                  String TempID='17818' ;
                  String MobileNo=mobile;
                  String F1=name;
                  String SenderID = 'SFDCEX';
                  String F2= '8-09-2013';
                  String F3= '07-09-2013';
       String postData = 'username=' +Username + '&pass=' + Password + '&dest_mobileno=' + MobileNo +'&senderid=' + SenderID + '&tempid=' + TempID+'&F1=' + F1+ '&F2=' + F2+ '&F3=' + F3;

       HttpRequest req = new HttpRequest();
    req.setEndpoint('http://126.67.38.46/blank/sms/xxxx/urlsmstemp.php?'+postdata);
    req.setMethod('GET');
    system.debug(req);
    Http http = new Http();
    HttpResponse res = http.send(req);
    return res;
    //system.debug(res);
    }

}

This is Trigger

trigger sms_3 on Account (After insert,After update) {

for(Account acc:trigger.new)
{
if(acc.check__c== true)

{

for(user u:[SELECT Alias,Id,MobilePhone,Name,Phone FROM User where Id=: acc.OwnerId])
{
if(u.id != trigger.oldmap.get(acc.id).ownerid)
{
 sendsms2.sendsms1(u.MobilePhone,u.name);

}//sendsms.sendsms1(acc.Phone1__c,acc.name);
}
}
}
}

This is my Test class

@isTest(seealldata=true)
public class sms1{

public static testmethod void testsms()
{
//user ur=[select id,Name from user where id=:'0059000000101C6'];
Account acc= new Account();
acc.name='Acc1';
acc.Status__c='new';
insert acc;
acc.Status__c='qualified';
update acc;
}
static testmethod void testMe() {
        Test.setMock(HttpCalloutMock.class, new Stubby());
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://123.63.33.43/blank/sms/user/urlsmstemp.php?');
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = sendsms.sendsms1('8015825819','sam');
        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());

}

}

This is my mock test class

@IsTest global class Stubby implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
       // System.assertEquals('http://123.63.33.43/blank/sms/xxxx/urlsmstemp.php?', 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;
    }
}
  • 1
    In your test class you have sendsms.sendsms1, and your actual class is called sendsms2. Could be a typo, but could also be your problem. Also, your trigger only does stuff if check__c == true, which you don't seem to set in your test. – rael_kid Jan 29 '14 at 14:39
  • I have a similar imp when contact email is updated it is to be sent to external system with email and contact id and first name fields. Any help how i can achieve this, Please Help me with code as Im new with integration. Thanks a lot in advance. Abraham – Abraham Jun 4 '15 at 21:37
4

If you separate the HTTP callout from the @future method, you can test them individually and get full coverage.

  1. make the @future method return void,

  2. use Test.startTest() and Test.stopTest() around its invocation in test (so it executes synchronously),

  3. use HttpCalloutMock (as you are) to cover the now-separate HTTP caller,

Note that you won't receive an SMS when your tests run due to the mock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.