+ Start a Discussion
nareshnaresh 

very urgent !!! when opportunity is created for account then he must get an sms. please suggest me how to write a test class???

task--  theren is account and a custom field rder number in opportunity when opportunity is created to account then he must notify with sms . sms contains the order number.

Please suggest me how to write a test class for below trigger

below is my trigger 

trigger SendSMS on Opportunity (after insert) {

for(Opportunity o : Trigger.new)
{
  if(o.OrderNumber__c!=null)
  {
   System.debug('************'+o.AccountId+'**********'+o.AccountId);
   if(o.AccountId!=null)
   {
    Account a = [select Id,phone from Account where Id=:o.AccountId];
    DisplaySMSQueueListViewRecords.SedingSMSManullay(o.OrderNumber__c,a.phone);
    }
    System.debug('********Later method****');
   }  


below is my class

public with sharing class DisplaySMSQueueListViewRecords 
{
    //variables
     @future(Callout=true)
    //Sending SMS Manually
    Public static void SedingSMSManullay(String msg,String phone)
    {               
    
                     String mobileNumber=phone;
                      String SendSMSString='Order number is -->'+msg;     
                        Http h = new Http();
                        HttpRequest req = new HttpRequest();
                        req.setEndpoint('http://bhashsms.in/api/sendmsg.php?user=demomask&pass=123&sender=DEMOMA&phone='+mobileNumber+'&text='+SendSMSString+'&priority=sdnd&stype=normal');    
                        req.setHeader('Accept', 'text/xml');
                        req.setHeader('Content-Type' , 'text/plain');
                        req.setMethod('GET');
                        HttpResponse res = h.send(req);
                       // Responsebody =res.getBody();
                        System.debug('***********'+res);
     }

      
 }


Please suggest me how to write a test class for the above trigger. Its very urgent.

​   Thanks in advance.

 
SarfarajSarfaraj
Hi Naresh

You can not do webservice callout from a test method. You have to mock it. I have modified your for DisplaySMSQueueListViewRecords. Change line 23 as per your requirement.
public with sharing class DisplaySMSQueueListViewRecords 
{
    //variables
     @future(Callout=true)
    //Sending SMS Manually
    Public static void SedingSMSManullay(String msg,String phone)
    {               
    
                     String mobileNumber=phone;
                      String SendSMSString='Order number is -->'+msg;     
                        Http h = new Http();
                        HttpRequest req = new HttpRequest();
                        req.setEndpoint('http://bhashsms.in/api/sendmsg.php?user=demomask&pass=123&sender=DEMOMA&phone='+mobileNumber+'&text='+SendSMSString+'&priority=sdnd&stype=normal');    
                        req.setHeader('Accept', 'text/xml');
                        req.setHeader('Content-Type' , 'text/plain');
                        req.setMethod('GET');
 						HttpResponse res;
        				if(!Test.isRunningTest())
                        	res = h.send(req);
        				else
                        {
                            res = new HttpResponse();
                            //write code to set status, body etc as you need with sample data.
                        }
                       // Responsebody =res.getBody();
                        System.debug('***********'+res);
     }      
 }
And here is the test class,
@isTest
public class SendSMSTest {
	public static testmethod void testSendSMS()
	{
    	Account a = new Account(Name = 'test');
    	insert a;
    	Opportunity o = new Opportunity(Name = 'Test', AccountId = a.Id, OrderNumber__c = 'test', StageName = 'Prospecting', CloseDate = System.TODAY());
    	insert o;
	}
}

--Akram