+ Start a Discussion
cml9cml9 

Need help in my Unit Test

For some reason I am stucked at 53% and I don't know what I am missing.

here is my trigger:
 
trigger activityTrigger on Task (after insert, after update) {
    set<id> oppIds = new set<id>();
    //map<id,Task> mapTask = new map<id,Task>();
    
    if(trigger.isafter){
        if(trigger.isInsert){
            for(Task loopTask:trigger.new){
                if(loopTask.WhatId != null && string.valueof(loopTask.whatid).startsWith('006')){
                    
                    oppIds.add(loopTask.WhatId);
                    //mapTask.put(loopTask.Id, loopTask);
                }
            }
            
            list<Opportunity> opportunityList = new list<Opportunity>([ SELECT id, test_first_activity__c, 
                                                                        (SELECT Id, Status,Subject,Type, CreatedDate, whatId  FROM Tasks order by CreatedDate asc limit 1) 
                                                                        FROM Opportunity Where id IN: oppIds ]); 
            list<Task> taskList = new list<Task>();
            
            if(oppIds.size() > 0){ 
                for(Opportunity loopOpp : opportunityList  ){
                    
                    if(loopOpp.getSobjects('Tasks') != null && loopOpp.getSobjects('Tasks').size() > 0) 
                    taskList = loopOpp.getSobjects('Tasks'); 
                    
                    if(taskList[0].whatId == loopOpp.id){
                        if(taskList[0].Status != null && taskList[0].Status.equalsignorecase('Completed') ){
                            loopOpp.test_first_activity__c = taskList[0].CreatedDate;
                        }
                    }
                }
            }
            update opportunityList;
        }
        
        if(trigger.isUpdate){
            for(Task loopTask:trigger.new){
                if(loopTask.WhatId != null && string.valueof(loopTask.whatid).startsWith('006')){
                    
                    oppIds.add(loopTask.WhatId);
                    //mapTask.put(loopTask.Id, loopTask);
                }
            }
            
            list<Opportunity> opportunityList = new list<Opportunity>([ SELECT id, test_first_activity__c, 
                                                                        (SELECT Id, Status,Subject,Type, CreatedDate, whatId  FROM Tasks order by CreatedDate asc limit 1) 
                                                                        FROM Opportunity Where id IN: oppIds ]); 
            list<Task> taskList = new list<Task>();
            
            if(oppIds.size() > 0){ 
                for(Opportunity loopOpp : opportunityList  ){
                    
                    if(loopOpp.getSobjects('Tasks') != null && loopOpp.getSobjects('Tasks').size() > 0) 
                    taskList = loopOpp.getSobjects('Tasks'); 
                    
                    if(taskList[0].whatId == loopOpp.id){
                        if(taskList[0].Status != null && taskList[0].Status.equalsignorecase('Completed') ){
                            loopOpp.test_first_activity__c = taskList[0].CreatedDate;
                        }
                    }
                }
            }
            update opportunityList;
        }
    }
    
}

And here is my unit test
 
@isTest
public class PounceTime_Test {
    
    static testMethod void PounceTime() {
        
         Account a = new Account();
   		 a.Name = 'Account';
        
        
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Kash';
        c.AccountId = a.Id;
        
        insert c;
        
        
        Opportunity o = new Opportunity();
        o.Name = 'MyOpps';
        o.StageName = 'New';
        o.CloseDate = Date.today().addDays(7);
        o.Discount__c = 1;
        o.test_first_activity__c = Datetime.now();
        o.Contact__c = c.Id;
       
      insert o;
   
      
        
        List<Task> tasks = new List<Task>();
		 tasks.add(new Task(
   		 ActivityDate = Date.today().addDays(7),
   		 Subject='Sample Task',
    	 WhatId = o.Id,
         WhoId = c.Id,
         Priority = 'Normal',
   		 OwnerId = UserInfo.getUserId(),
    	 Status='In Progress'));

		insert tasks;
    }

}

Thank you in advance.!
Best Answer chosen by cml9
swati_sehrawatswati_sehrawat
@isTest
public class PounceTime_Test {
    
    static testMethod void PounceTime() {
        
         Account a = new Account();
         a.Name = 'Account';
        
        
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Kash';
        c.AccountId = a.Id;
        
        insert c;
        
        
        Opportunity o = new Opportunity();
        o.Name = 'MyOpps';
        o.StageName = 'New';
        o.CloseDate = Date.today().addDays(7);
        
        o.test_first_activity__c = Datetime.now();
        
       
      insert o;
   
      
        
        List<Task> tasks = new List<Task>();
         tasks.add(new Task(
         ActivityDate = Date.today().addDays(7),
         Subject='Sample Task',
         WhatId = o.Id,
         WhoId = c.Id,
         Priority = 'Normal',
         OwnerId = UserInfo.getUserId(),
         Status='Completed'));

        insert tasks;
        
        tasks[0].Status = 'Completed';
        update tasks;
    }

}

Try This and let me know if it works.

All Answers

swati_sehrawatswati_sehrawat
@isTest
public class PounceTime_Test {
    
    static testMethod void PounceTime() {
        
         Account a = new Account();
         a.Name = 'Account';
        
        
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Kash';
        c.AccountId = a.Id;
        
        insert c;
        
        
        Opportunity o = new Opportunity();
        o.Name = 'MyOpps';
        o.StageName = 'New';
        o.CloseDate = Date.today().addDays(7);
        
        o.test_first_activity__c = Datetime.now();
        
       
      insert o;
   
      
        
        List<Task> tasks = new List<Task>();
         tasks.add(new Task(
         ActivityDate = Date.today().addDays(7),
         Subject='Sample Task',
         WhatId = o.Id,
         WhoId = c.Id,
         Priority = 'Normal',
         OwnerId = UserInfo.getUserId(),
         Status='Completed'));

        insert tasks;
        
        tasks[0].Status = 'Completed';
        update tasks;
    }

}

Try This and let me know if it works.
This was selected as the best answer
cml9cml9
Thanks man! 100%!