+ Start a Discussion
Lee.ax1423Lee.ax1423 

Unit Test cases

Can any one write Unit test cases for following code.....

 

 

public PageReference SendSMS() {

if (a != null) {

//construct an HTTP request
HttpRequest req = new HttpRequest();

req.setEndpoint('sample endpoint');
req.setMethod('GET');

 

//send the request
Http http = new Http();
HttpResponse res = http.send(req);

 

//check the response
if (res.getStatusCode() == 200) {

   smsTask = new Task (Type='SMS');
try {
insert smsTask;
} catch (System.Dmlexception e) {
System.debug('Error: Unable to insert task: ' + e);
}
PageReference pg = new PageReference('/'+smsTask.Id);
return pg;

} else {
ApexPages.Message didNotSendMsg = new ApexPages.Message(ApexPages.severity.Info, 'some message');
ApexPages.addMessage(didNotSendMsg);
return null;
}
}
else {
ApexPages.Message noMobileMsg = new ApexPages.Message(ApexPages.severity.Info,'some message');
ApexPages.addMessage(noMobileMsg);
return null;
}

 

Help me!!!

asish1989asish1989

Hi 

   Try this ... 

  You need to care about 'a'.First set value of a !=null and then a= null .Rest I have written .please check ..may be I have left something .

 

      

private Static void testmethod() {
       HttpRequest req = new HttpRequest();

req.setEndpoint('sample endpoint');
req.setMethod('GET');
req.estStatusCode =200 ;
Http http = new Http()
HttpResponse res = http.send(req);
Contact con = new Conatct(LastName = 'test');
insert con;
Task newtask = new Task(Type='SMS',
WhoID = con.id,
tatus = 'testing',
ActivityDate = System.today(),
Description = msg,
Subject = 'SMS Sent');

insert newtask;
HttpRequest req1 = new HttpRequest();

req1.setEndpoint('sample endpoint');
req1.setMethod('GET');
req1.estStatusCode =500 ;
Http http = new Http()
HttpResponse res = http.send(req1);
Account acc = new Account(Name = 'set');
insert acc;
Task newtask1 = new Task(Type='SMS',
WhoID = acc.id,
tatus = 'testing',
ActivityDate = System.today(),
Description = msg,
Subject = 'SMS Sent');

insert newtask1;
YourClassName newobj = YourClassName();
newobj.SendSMS();
}

 

 

Did this post solve your problem If so please mark it solved otherwise let me know about your Issue

 

 

Thanks

asish

 

Lee.ax1423Lee.ax1423

Hi asish,I got this error

 

Error: Compile Error: Method does not exist or incorrect signature: [System.HttpRequest].getStatusCode() .

asish1989asish1989

Hi

   Try by using this

           HttpResponse res = new HttpResponse(); 

            res.setStatusCode = 200;

            res = http.send(req);

 

Please modify code. Delete this line req.setStatusCode = 200; in two place .

 

 

let me know about error ...

 

 

thanks

asish

 

Lee.ax1423Lee.ax1423
It is showing following error. Error: Compile Error: Method does not exist or incorrect signature: [System.HttpResponse].setStatusCode(). MY code is here. HttpRequest req = new HttpRequest(); ?? ? ? ?req.setEndpoint(endpoint+'http://www.google.com'); ?? ? ? ?req.setMethod('GET'); ?? ? ? ?//req.getStatusCode() =200 ; ?? ? ? ?Http http = new Http(); ?? ? ? ?HttpResponse res = new HttpResponse(); ?? ? ? ?res.setStatusCode=200; ?? ? ? ?res=http.send(req);
asish1989asish1989

Hi

  Sory to say I have no idea how to provide StatusCode .you have to set first StatusCode = 200 and then StatusCode != 200

to cover else block .

 

 

 

Thanks

asish

Lee.ax1423Lee.ax1423
Ya ok thank you... If i set status code i will achieve my code coverage...Here only i am getting problem. thanks.
asish1989asish1989

Hi

  Try by using this

 

 System.RestResponse.statusCode = 200;

 

for more information

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_restresponse.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_methods_system_restresponse.htm|StartTopic=Content%2Fapex_methods_system_restresponse.htm|SkinName=webhelp

 

Did this post answers your questions ...If so please mark it solved so that It can be refered .

Thanks

asish