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
Jonathan WallaceJonathan Wallace 

Need help with a test class for my trigger

New to triggers, and i've finally got my trigger working as needed in the sandbox. 
here it is .. 
 
trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
    for(Opportunity opp : Trigger.New) {
        Id closedBy; 
        
        Date closedOn = opp.CreatedDate.date(); 
        
        if (opp.IsClosed) {
          
            closedBy = opp.LastModifiedById;
            closedOn = opp.LastModifiedDate.date();
            
  
            if (Trigger.isUpdate) {
                Opportunity old = Trigger.OldMap.get(opp.Id);
                if (old.IsClosed == opp.IsClosed) {
                    closedBy = old.Closed_By__c;
                    closedOn = old.CloseDate;
                }
            }   
        } 
        opp.Closed_By__c = closedBy;
        opp.CloseDate = closedOn;
    }
}
My test class isn't working, i'm getting "Your ClassID property is missing or null" error from Force IDE
here is my class
@isTest

public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
       
      Opportunity opp = new Opportunity();
       
      opp.Name= 'Opportunity';
      opp.StageName='Closed Grant';
      opp.CloseDate=System.today();
                                       
        Update opp;
        
        
        

    
    }
    
}

Please let me know if there is something i'm missing, i'm still learning 

 
Best Answer chosen by Jonathan Wallace
GauravGargGauravGarg
Jonathan,

Please use below apex class and test class code. 
Apex class:
trigger ClosedOpportunityTrigger1 on Opportunity (before insert, before update) {
    for(Opportunity opp : Trigger.New) {
        Id closedBy; 
        system.debug(' new trigger: '+opp);
     	Date closedOn;
        if (Trigger.isInsert) 
            closedOn = Date.today();
        else
     		closedOn = opp.CreatedDate.date(); 
        
        if (opp.IsClosed) {
          
            closedBy = opp.LastModifiedById;
            closedOn = opp.LastModifiedDate.date();
            
  
            if (Trigger.isUpdate) {
                Opportunity old = Trigger.OldMap.get(opp.Id);
                if (old.IsClosed == opp.IsClosed) {
                    closedBy = old.Closed_By__c;
                    closedOn = old.CloseDate;
                }
            }   
        } 
        opp.Closed_By__c = closedBy;
        opp.CloseDate = closedOn;
    }
}

Test Class:
@isTest
public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
     Account acct = new Account(Name='Test Account');
     insert acct;
    
     Opportunity opp = new Opportunity();
      opp.Name= 'Opportunity';
      opp.StageName='Prospecting';
      opp.CloseDate=System.today();
      insert opp; 
	  
	  opp.StageName='Closed Won';
	  update opp;
      
      opp.closeDate = system.Today();
      update opp;

	}
}

This is covering 100% in my org. 

Please let me know if you need assitance on this or contact me on below details:

Thanks
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990

All Answers

Dilip_VDilip_V
Hi Jonathan,

Your are updating opportunity in your test class with out inserting it.First insert the opportunity and then update the opportunity.

 
@isTest

public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
       
      Opportunity opp = new Opportunity();
       
      opp.Name= 'Opportunity';
      opp.StageName='Closed Grant';
      opp.CloseDate=System.today();
                                       
        //Update opp;//This is wrong because it doesn't have a ID.
       insert opp;
        //Here you can update opportunity.
      
    }
    
}

 
Jonathan WallaceJonathan Wallace
do i need to specify an id for a specific Opportunity?  
Jonathan WallaceJonathan Wallace
still get the same error
@isTest

public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
     
     Account acct = new Account(Name='Test Account');
     insert acct;
     
       
      Opportunity opp = new Opportunity();
       
      opp.Name= 'Opportunity';
      opp.StageName='Closed Grant';
      opp.CloseDate=System.today();
      opp.opportunity=opportunityId;
                                             
        
       insert opp; update opp;

}
}

 
Pankaj_GanwaniPankaj_Ganwani
Hi Jonathan,

Please use the below mentioned code:
@isTest

public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
     
     Account acct = new Account(Name='Test Account');
     insert acct;
     
       
      Opportunity opp = new Opportunity();
       
      opp.Name= 'Opportunity';
      opp.StageName='Closed Grant';
      opp.CloseDate=System.today();
                                             
        
       insert opp;

update opp;

}
}

 
GauravGargGauravGarg
Hi Jonathan,

Please try below code. 
@isTest

public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
     
     Account acct = new Account(Name='Test Account');
     insert acct;
     
       
      Opportunity opp = new Opportunity();
       
      opp.Name= 'Opportunity';
      opp.StageName='Closed Grant';
      opp.CloseDate=System.today();
	  insert opp; 
	  
	  Opp.name = 'update Opportunity';
	  update opp;

}
}

Let me know if you still face some issues. 

Thanks,
Gaurav
Jonathan WallaceJonathan Wallace
This is the error i get when trying to test it in Force IDE

User-added image
GauravGargGauravGarg
Hi Jonathan,

Could you please try saving the class file directly on your salesforce org. Or please refersh the code from org and then save. 

Thanks,
Gaurav
Jonathan WallaceJonathan Wallace
Ok, so i don't get the error anymore, but the test results are pulling in other classes that i didn't select? User-added image
Jonathan WallaceJonathan Wallace
And.. my class isn't listed
GauravGargGauravGarg
Jonathan,

Your class "ClosedOpportunityTrigger" is on the second line having 23% code i.e. 3/13 lines. 

Can you please check which all lines aren't covered using testMethod.
Please follow steps to find the lines not covered:

1. Open developer console.
2. ctrl + shift + O (chracter O).
3. Enter class name "ClosedOpportunityTrigger".
4. Click on code coverage button and check.
     Blue lines are the covered lines.
     Red lines are which need to be covered. 

Let me know if you need on this. 

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990
Jonathan WallaceJonathan Wallace
Here are the uncovered lines from the trigger.. 

User-added image
GauravGargGauravGarg
Jonathan,

Please use below apex class and test class code. 
Apex class:
trigger ClosedOpportunityTrigger1 on Opportunity (before insert, before update) {
    for(Opportunity opp : Trigger.New) {
        Id closedBy; 
        system.debug(' new trigger: '+opp);
     	Date closedOn;
        if (Trigger.isInsert) 
            closedOn = Date.today();
        else
     		closedOn = opp.CreatedDate.date(); 
        
        if (opp.IsClosed) {
          
            closedBy = opp.LastModifiedById;
            closedOn = opp.LastModifiedDate.date();
            
  
            if (Trigger.isUpdate) {
                Opportunity old = Trigger.OldMap.get(opp.Id);
                if (old.IsClosed == opp.IsClosed) {
                    closedBy = old.Closed_By__c;
                    closedOn = old.CloseDate;
                }
            }   
        } 
        opp.Closed_By__c = closedBy;
        opp.CloseDate = closedOn;
    }
}

Test Class:
@isTest
public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
     Account acct = new Account(Name='Test Account');
     insert acct;
    
     Opportunity opp = new Opportunity();
      opp.Name= 'Opportunity';
      opp.StageName='Prospecting';
      opp.CloseDate=System.today();
      insert opp; 
	  
	  opp.StageName='Closed Won';
	  update opp;
      
      opp.closeDate = system.Today();
      update opp;

	}
}

This is covering 100% in my org. 

Please let me know if you need assitance on this or contact me on below details:

Thanks
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990
This was selected as the best answer
Jonathan WallaceJonathan Wallace
when i try to save the class to my org i get
"Save error: Invalid constructor syntax, name=value pairs can only be used for SObjects    OppCloseDateTest.cls    /Closed Date Trigger/src/classes    line 5   "

 
 
GauravGargGauravGarg
Hi Jonathan,

Do you have AccountTrigger having controller which access Name as parameters. If not, please try below code. 
@isTest
public class OppCloseDateTest {

    static testMethod void TestChangeOpportunityClosedby() {
     Account acct = new Account();
     acct.Name='Test Account';
     insert acct;
    
     Opportunity opp = new Opportunity();
      opp.Name= 'Opportunity';
      opp.StageName='Prospecting';
      opp.CloseDate=System.today();
      insert opp; 
	  
	  opp.StageName='Closed Won';
	  update opp;
      
      opp.closeDate = system.Today();
      update opp;

	}
}

Please try if you still have any required field on Account object. If so, please add those field in Account test data.
Account acct = new Account();
acct.Name='Test Account';
// Insert required field here.
insert acct;

Please let me know if you still face issues on this, or you can contact me on below details.

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990