+ Start a Discussion
Amit Jadhav 13Amit Jadhav 13 

Can You Any One Help Me On The Below Trigger to Write Test Class

trigger UpdateOppOwner on Project__c (before update, after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    Map<Id,List<Opportunity>> OpportunityMap = new Map<Id,List<Opportunity>>();
    
    if(Trigger.isUpdate){
         
        for(Opportunity opp : [SELECT Id,Project__r.id,StageName,Type FROM Opportunity WHERE Project__r.id IN :Trigger.newMap.keySet()])
        {
            if(!OpportunityMap.containskey(opp.Project__c)){
                OpportunityMap.put(opp.Project__r.id,new List<Opportunity>());
            }
            OpportunityMap.get(opp.Project__r.id).add(opp);
            system.debug('OpportunityMap'+OpportunityMap);
        }
    }
    
               for(Project__c c:Trigger.new) {
                   
                   
                   if(OpportunityMap.get(c.Id) != Null){
                       for(Opportunity opp : OpportunityMap.get(c.Id)){ //get all contacts under account and update.
                           if(opp.StageName != 'Closed Won' || opp.StageName != 'Closed Lost'){
                               if(opp.Type == 'Renewal'){
                                   opp.OwnerId = c.Renewal_Owner__c;
                                   oppList.add(opp);
                                   system.debug('oppList'+oppList);
                               }else{
                                   opp.OwnerId = c.Opportunity_Owner__c;
                                   oppList.add(opp);
                                   system.debug('oppList'+oppList);
                               }
                           }
                           
                          
                       }
                   }
                   
               }
               
          
               try {
                   upsert oppList;
               } catch (Exception ex) {
                   System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());}
               }
Test Class
@isTest
public class TestUpdateOppOwner {
    
    static testMethod void testMethod1()
    {
        Account objacc = new Account(
            Name = 'Test'
        );
        insert objacc;
        
        Project__c objPro = new Project__c(
            Name = 'Test Demo',
            Type__c = 'Membership',
            Project_Type__c = 'Project',
            Category__c = 'Incorporated'           
        );
         insert objPro;
        
        Opportunity objOpp = new Opportunity(
            Name = 'Demo',
            AccountId = objacc.Id,
            CloseDate= System.today(),
            StageName = '3.Quote',
            ForecastCategoryName = 'Pipeline',
            PaymentTerms__c = 'Net 30',
            Project__c =objPro.id 
        );
        
       
       
        insert objOpp;
        
        
    }
    
}

 
Maharajan CMaharajan C
Hi Amit,

Try the below test class:

You have to update the Project record to cover the trigger.

And create one more method in test class but use the different Opportunity Type in test record to cover the else logic line no : 27 .
 
@isTest
public class TestUpdateOppOwner {
    
    static testMethod void testMethod1()
    {
	
		Profile p = [SELECT Id FROM profile 
                     WHERE name='System Administrator']; 
        
        User accUser = new User(alias = 'newUser1', 
                                email='accuser@testorg.com', 
                                emailencodingkey='UTF-8', lastname='Testing', 
                                languagelocalekey='en_US', 
                                localesidkey='en_US', profileid = p.Id, 
                                timezonesidkey='America/Los_Angeles', 
                                username='accuser@testorg.com');
        insert accUser;

		Test.startTest();  
        Account objacc = new Account(
            Name = 'Test'
        );
        insert objacc;
        
        Project__c objPro = new Project__c(
            Name = 'Test Demo',
            Type__c = 'Membership',
            Project_Type__c = 'Project',
            Category__c = 'Incorporated' 
			Renewal_Owner__c = accUser.Id;
        );
         insert objPro;
        
        Opportunity objOpp = new Opportunity(
            Name = 'Demo',
            AccountId = objacc.Id,
            CloseDate= System.today(),
            StageName = '3.Quote',
            ForecastCategoryName = 'Pipeline',
            PaymentTerms__c = 'Net 30',
            Project__c =objPro.id,
			Type = 'Renewal'
        );
               
        insert objOpp;
		
		objPro.Name = 'Test Demo Update';
		Update objPro;
		
        Test.stopTest();  
        
    }
    
}


Thanks,
Maharajan.C