function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lee.ax1423Lee.ax1423 

Unit test cases

Hi,

 

I am facing problem with unit test cases.....Recently i have integrated salesforce with outer environment.

but the problem is i got the test failure message while writing unit test cases for that.

System.NullPointerException: Attempt to de-reference a null object

Class.SendSMSHttpContact.<init>: line 9, column 1 Class.TestSendSMS.testUnas1: line 15, column 1

 

Code is:

 

public class SendSMSHttpContact {

private final Contact contact;
private final String user='some name';
private final String senderID='some';
public String receipientno{get;set;}
private final String dcs='0';
public String msgtxt{get;set;}
public String msg = msgtxt.replace(' ','%20');
private final String state='0';
String endpoint = 'sample url';

public SendSMSHttpContact(ApexPages.StandardController controller) {
this.contact = (Contact)controller.getRecord();
}

public PageReference SendSMS() {

if (contact.MobilePhone != null) {

receipientno = contact.MobilePhone;
receipientno = receipientno.replace('+','');
receipientno = receipientno.replace(' ','');
receipientno = receipientno.replace(')','');
receipientno = receipientno.replace('(','');
receipientno = receipientno.replace('-','');

//construct an HTTP request
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(endpoint+'?user='+user+
'&senderID='+senderID+
'&receipientno='+receipientno+
'&dcs='+dcs+
'&msgtxt='+msg+
'&state='+state);

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

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

Task smsTask = new Task (Type='SMS',
WhoID = contact.id,
Status = 'Completed',
ActivityDate = System.today(),
Description = msg.replace('%20',' '),
Subject = 'SMS Sent'
);
try {
insert smsTask;
} catch (System.Dmlexception e) {
System.debug('Error: Unable to insert task: ' + e);
}
PageReference pg = new PageReference('/'+smsTask.Id);
return pg;
} else {

//do something
}
}
else {

//do something
}}}

 

 

unit test coding is:

 

@isTest
private class TestSendSMS{
private static testMethod void testUnas1(){


String endpoint='http://google.com';
Contact con = new Contact();
con.LastName = 'test';
con.MobilePhone ='12345';
insert con;

ApexPages.StandardController controller = new ApexPages.StandardController(con);
SendSMSHttpContact test = new SendSMSHttpContact(controller);
system.assertEquals(Page.success,test.SendSMS());

}}

 

 

 

i am in basic stage in writting unit test cases.

can you help me to write unit test case for this.

 

Past two days onwards i am searching for this.

 

can any one help me to get out of this.!!!!!