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
Sai Ram 118Sai Ram 118 

Test Coverage Help?

Hi EveryOne,
Test class Help


global with sharing class LeaseApproval_SendNotification {
    
    public ID leaseAnalysisId    {get;set;}
    public Opportunity opp         {get;set;}
    public Lease_Analysis__c la {get;set;}
    public List<ID> scenarioIds {get;set;}

    public String richComments        {get;set;}

    public LeaseApproval_SendNotification() {
        
    }

    
    
    @RemoteAction
    global static ResultMessage sendNotification(List<String> toEmails, List<String> ccEmails, List<String> bccEmails, String comments, String oppName, String oppId, List<ID> scenarios){
        try{
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            //TODO create the notification record and junction objects
            if(toEmails.size()>0)
            mail.setToAddresses(toEmails);
            if(ccEmails.size()>0)
            mail.setCcAddresses(ccEmails);
            if(bccEmails.size()>0)
            mail.setBccAddresses(bccEmails);

            Messaging.reserveSingleEmailCapacity(1);

            String body = comments;

            String leaseAnalysisLink='<a href="'+URL.getSalesforceBaseUrl().toExternalForm()+'/apex/LeaseAnalysis?opportunityId='+oppId+'">Lease Analysis - '+oppName+'</a><br/>';

            mail.setHTMLBody('You have been notified about a deal ('+leaseAnalysisLink+') from '+userInfo.getFirstName() + ' ' + userInfo.getLastName() +'.  No action is required. \r\n' + body);

            mail.setSubject('NOTIFICATION ALERT from '+userInfo.getFirstName() + ' ' + userInfo.getLastName()+' – '+ oppName);

            //TODO - make for loop over scenario ids to create a pdf per scenario
            PageReference pr = page.LeaseApproval_Notification;

            Blob attachmentBody = pr.getContentAsPDF();
            
            Messaging.EmailFileAttachment att = new Messaging.EmailFileAttachment();
            att.setBody(attachmentBody);
            att.setFileName('Scenario Attachment.pdf');

            mail.setFileAttachments(new List<Messaging.EmailFileAttachment>{att});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            return new ResultMessage(true, 'successfully sent');
        }
        catch(Exception e){
            return new ResultMessage(false, e.getMessage() +  '\n ' + e.getStackTraceString());
        }
        return null;
    }
}
Raj VakatiRaj Vakati
try this code
 
@isTest
private class LeaseApproval_SendNotificationTest {

static testmethod void test() {        
	Test.startTest();
	  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Closed Lsot',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;


		
	
	
	
	
	Lease_Analysis__c  lc = new Lease_Analysis__c();
	lc.Name ='Test' ;
	lc.Opportunity__c = oppt.Id ; 
	// add other fields 
	insert lc ;
	CDP_Deal__c dl = new CDP_Deal__c() ;
	dl.Name ='Test';
	dl.Stage__c='Won - Pending Yardi';
	// add other fields 
	insert dl ; 
	
	
	CDP_Deal_BTS_Lease__c  cdpD = new CDP_Deal_BTS_Lease__c ();
	cdpD.Name ='Test';
	// add other fields 
	cdpD.CDP_Deal__c =dl.Id ;
	insert cdpD ;
	
	Scenario__c  sc = new Scenario__c () ;
	sc.Name ='Test' ;
	sc.Lease_Analysis__c = lc.Id ;
	sc.Yardi_Deal_Terms__c  =yc.id ;
	insert sc ;
	// add other required fileds 
	
	
	
	
	List<String> toEmails = new List<String>() ; 
	toEmails.add('test@mail.com') ;
	
	List<String> ccEmails =new List<String>() ;
	ccEmails.add('test1@mail.com') ;
	
	List<String> bccEmails new List<String>() ;
	bccEmails.add('test2@mail.com') ;
	
	String comments='WORING ' ;
	String oppName = oppt.Name ; 
	String oppId = oppt.Id ; 
	List<ID> scenarios = new List<ID>();
	scenarios.add(sc) ;
	LeaseApproval_SendNotification.sendNotification(toEmails,ccEmails,bccEmails,  comments,  oppName,  oppId, scenarios);
	
	
	
	Test.stopTest();
  
}

}

 
Sai Ram 118Sai Ram 118
Thanks Raj V for helping me with test class.

When i use this code i am gettingn these errors

Error: Compile Error: Unexpected token 'List'. at line 58 column 5
Error: Compile Error: Method does not exist or incorrect signature: void startTest() from the type Test at line 5 column 10
 
Raj VakatiRaj Vakati
try this code
 
@isTest
private class LeaseApproval_SendNotificationTest {

static testmethod void test() {        
	Test.startTest();
	  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Closed Lsot',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;


		
	
	
	
	
	Lease_Analysis__c  lc = new Lease_Analysis__c();
	lc.Name ='Test' ;
	lc.Opportunity__c = oppt.Id ; 
	// add other fields 
	insert lc ;
	CDP_Deal__c dl = new CDP_Deal__c() ;
	dl.Name ='Test';
	dl.Stage__c='Won - Pending Yardi';
	// add other fields 
	insert dl ; 
	
	
	CDP_Deal_BTS_Lease__c  cdpD = new CDP_Deal_BTS_Lease__c ();
	cdpD.Name ='Test';
	// add other fields 
	cdpD.CDP_Deal__c =dl.Id ;
	insert cdpD ;
	
	Scenario__c  sc = new Scenario__c () ;
	sc.Name ='Test' ;
	sc.Lease_Analysis__c = lc.Id ;
	sc.Yardi_Deal_Terms__c  =yc.id ;
	insert sc ;
	// add other required fileds 
	
	
	
	
	List<String> toEmails = new List<String>() ; 
	toEmails.add('test@mail.com') ;
	
	List<String> ccEmails =new List<String>() ;
	ccEmails.add('test1@mail.com') ;
	
	List<String> bccEmails= new List<String>() ;
	bccEmails.add('test2@mail.com') ;
	
	String comments='WORING ' ;
	String oppName = oppt.Name ; 
	String oppId = oppt.Id ; 
	List<ID> scenarios = new List<ID>();
	scenarios.add(sc) ;
	LeaseApproval_SendNotification.sendNotification(toEmails,ccEmails,bccEmails,  comments,  oppName,  oppId, scenarios);
	
	
	
	Test.stopTest();
  
}

}

 
Sai Ram 118Sai Ram 118
Hi Raj
getting same error 


Error: Compile Error: Method does not exist or incorrect signature: void startTest() from the type Test at line 5 column 10
Raj VakatiRaj Vakati
Use this
 
@isTest
private class LeaseApproval_SendNotificationTest {

static testmethod void testApproavl() {        
	System.Test.startTest();
	  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Closed Lsot',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;


		
	
	
	
	
	Lease_Analysis__c  lc = new Lease_Analysis__c();
	lc.Name ='Test' ;
	lc.Opportunity__c = oppt.Id ; 
	// add other fields 
	insert lc ;
	CDP_Deal__c dl = new CDP_Deal__c() ;
	dl.Name ='Test';
	dl.Stage__c='Won - Pending Yardi';
	// add other fields 
	insert dl ; 
	
	
	CDP_Deal_BTS_Lease__c  cdpD = new CDP_Deal_BTS_Lease__c ();
	cdpD.Name ='Test';
	// add other fields 
	cdpD.CDP_Deal__c =dl.Id ;
	insert cdpD ;
	
	Scenario__c  sc = new Scenario__c () ;
	sc.Name ='Test' ;
	sc.Lease_Analysis__c = lc.Id ;
	sc.Yardi_Deal_Terms__c  =yc.id ;
	insert sc ;
	// add other required fileds 
	
	
	
	
	List<String> toEmails = new List<String>() ; 
	toEmails.add('test@mail.com') ;
	
	List<String> ccEmails =new List<String>() ;
	ccEmails.add('test1@mail.com') ;
	
	List<String> bccEmails= new List<String>() ;
	bccEmails.add('test2@mail.com') ;
	
	String comments='WORING ' ;
	String oppName = oppt.Name ; 
	String oppId = oppt.Id ; 
	List<ID> scenarios = new List<ID>();
	scenarios.add(sc) ;
	LeaseApproval_SendNotification.sendNotification(toEmails,ccEmails,bccEmails,  comments,  oppName,  oppId, scenarios);
	
	
	
	System.Test.stopTest();
  
}

}

 
Sai Ram 118Sai Ram 118
Hi Raj
getting error

Error: Compile Error: Method does not exist or incorrect signature: void add(Scenario__c) from the type List<Id> at line 65 column 15
Raj VakatiRaj Vakati
Last try  ..use this code
 
@isTest
private class LeaseApproval_SendNotificationTest {

static testmethod void testApproavl() {        
	System.Test.startTest();
	  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Closed Lsot',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;


		
	
	
	
	
	Lease_Analysis__c  lc = new Lease_Analysis__c();
	lc.Name ='Test' ;
	lc.Opportunity__c = oppt.Id ; 
	// add other fields 
	insert lc ;
	CDP_Deal__c dl = new CDP_Deal__c() ;
	dl.Name ='Test';
	dl.Stage__c='Won - Pending Yardi';
	// add other fields 
	insert dl ; 
	
	
	CDP_Deal_BTS_Lease__c  cdpD = new CDP_Deal_BTS_Lease__c ();
	cdpD.Name ='Test';
	// add other fields 
	cdpD.CDP_Deal__c =dl.Id ;
	insert cdpD ;
	
	Scenario__c  sc = new Scenario__c () ;
	sc.Name ='Test' ;
	sc.Lease_Analysis__c = lc.Id ;
	sc.Yardi_Deal_Terms__c  =yc.id ;
	insert sc ;
	// add other required fileds 
	
	
	
	
	List<String> toEmails = new List<String>() ; 
	toEmails.add('test@mail.com') ;
	
	List<String> ccEmails =new List<String>() ;
	ccEmails.add('test1@mail.com') ;
	
	List<String> bccEmails= new List<String>() ;
	bccEmails.add('test2@mail.com') ;
	
	String comments='WORING ' ;
	String oppName = oppt.Name ; 
	String oppId = oppt.Id ; 
	List<ID> scenarios = new List<ID>();
	scenarios.add(sc.Id) ;
	LeaseApproval_SendNotification.sendNotification(toEmails,ccEmails,bccEmails,  comments,  oppName,  oppId, scenarios);
	
	
	
	System.Test.stopTest();
  
}

}

 
Sai Ram 118Sai Ram 118
thanks very much raj Raj 
Class saved but test failed may be because some fields are missing
if add mandatory it will pass right?

 
Raj VakatiRaj Vakati
yes .. what is the error message you are getting ?? add all required fields and try 
Raj VakatiRaj Vakati
Is its resolved ??
Sai Ram 118Sai Ram 118
It is giving error
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Area (SF) field is required. Please insert a value and save.: [Area_SF__c]
Raj VakatiRaj Vakati
You need to insert Area_SF__c in to the test class .. please add all the required fields and data setup..
Sai Ram 118Sai Ram 118
Hi Raj,
I given all required fields but it is giving some upsert error. Any trigger should not effect on test class right but

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityTrigger: execution of BeforeInsert

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

Class.UOMCalculation.getTsuboFromSf: line 25, column 1
Trigger.OpportunityTrigger: line 58, column 1: []

Here is my updated code



@isTest
private class LeaseApproval_SendNotificationTest {

static testmethod void testApproavl() {        
    System.Test.startTest();
      Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Closed Lsot',
                            Amount = 3000,
                            Area_SF__c=0,
                            CloseDate = System.today());

   insert oppt;
  

    Lease_Analysis__c  lc = new Lease_Analysis__c();
    lc.Name ='Test' ;

    lc.Opportunity__c = oppt.Id ; 
    // add other fields 
    insert lc ;
    CDP_Deal__c dl = new CDP_Deal__c() ;
    dl.Name ='Test';
    dl.Stage__c='Won - Pending Yardi';
    // add other fields 
    insert dl ; 
    
    
    CDP_Deal_BTS_Lease__c  cdpD = new CDP_Deal_BTS_Lease__c ();
    cdpD.Name ='Test';
    
    cdpD.CDP_Deal__c =dl.Id ;
    insert cdpD ;
    
    Scenario__c  sc = new Scenario__c () ;
    sc.Name ='Test' ;
    sc.Lease_Analysis__c = lc.Id ;
    sc.Yardi_Deal_Terms__c  =lc.id ;
    insert sc ;
    // add other required fileds 
    
    
    
    
    List<String> toEmails = new List<String>() ; 
    toEmails.add('test@mail.com') ;
    
    List<String> ccEmails =new List<String>() ;
    ccEmails.add('test1@mail.com') ;
    
    List<String> bccEmails= new List<String>() ;
    bccEmails.add('test2@mail.com') ;
    
    String comments='WORING ' ;
    String oppName = oppt.Name ; 
    String oppId = oppt.Id ; 
    List<ID> scenarios = new List<ID>();
    scenarios.add(sc.Id) ;
    LeaseApproval_SendNotification.sendNotification(toEmails,ccEmails,bccEmails,  comments,  oppName,  oppId, scenarios);
    
    
    
    System.Test.stopTest();
  
}

}
Raj VakatiRaj Vakati
Add all the reiquired fileds on Opportunity and try .. 

The error is coming from the Opportunity trigger