-2

Below trigger to increase the code coverage,as of now i got 70% code coverage.Please refer below test class.

@isTest(seeAllData = true)
private class TestTask{    
    static testMethod void TestTask_1(){
         Account acc = new Account();            
            acc.Name='TestAcc';                    
            acc.salesAreaList__c = '';            
            acc.BillingCountry = 'India';            
            acc.BillingCity = 'test city';            
            acc.BillingStreet = 'test street';            
            acc.ShippingCountry = 'India';                     
            insert acc; 
        Opportunity Opp = new Opportunity();
            Opp.name = 'Sample Opportunity';
            Opp.AccountId = acc.id;
            Opp.StageName = 'Prospecting';
            Opp.ForecastCategoryName = 'Pipeline';
            Opp.CloseDate = System.Today();
            Opp.Amount = 100;
            Opp.Probability = 10;            
            Opp.Help__c ='Test';
            Opp.Description__c = 'Help' ;
            Insert Opp;  
        Task t1 = new Task();
          t1.OwnerId = UserInfo.getUserId();
          t1.WhatId = opp.Id;
          t1.Subject ='Help';             
          t1.ActivityDate = System.Today();  
          t1.Description = 'Comments';
          t1.Status = 'In Progress';
          t1.Priority = 'Normal';
          Insert t1;
         t1.ActivityDate = System.Today().addDays(7);
         t1.Subject ='QMIHelp'; 
         t1.Description = 'Help';
         Update t1;  
    }
 }

Trigger Code:

Trigger Task on Opportunity (after insert,after update){    
    List<Opportunity> listOpp = Trigger.new;
    List<Task> listTask = new List<Task>();    
       if(Trigger.isInsert){
        for(Opportunity opp:listOpp){              
           if(opp.Help__c != null){
                Task t = new Task();
                t.OwnerId = opp.OwnerId;
                t.WhatId = opp.Id;
                t.Subject ='Help' + ': ' +opp.Help__c ; 
                t.ActivityDate = System.Today()+7;  
                t.Description = opp.Description__c;
                t.Status = 'In Progress';
                t.Priority = 'Normal';
                listTask.add(t);
            } 
        }
    }   

     if(Trigger.isUpdate){
         Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
        for(Opportunity opp:listOpp){
            if(Opp.Help__c != oldOppMap.get(opp.Id).Help__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c ){
            Task t = new Task();
            t.WhatId = opp.Id;   
            t.Subject = 'Help' + ': ' +opp.Help__c ; 
            t.Description = opp.Description__c;  
            t.ActivityDate = System.Today()+7;
            t.Status = 'In Progress';
            t.Priority = 'Normal';        
            listTask.add(t);
         }
        }   
     }  
     insert listTask;
 }
4
  • 5
    Your not testing anything here. You are just causing the code to run over lines. And you are not updating the opp or changing the value of Help__c/Description which is a requirement in your trigger. Review your code, see what is required, do it, then assert that the appropriate tasks are created with the appropriate values
    – Eric
    Aug 23, 2016 at 7:39
  • 5
    Also @isTest(seeAllData = true) is not needed here. Using it have very few use cases. Aug 23, 2016 at 7:42
  • Hi @Eric , Thanks for your reply.. But as of now am getting 70% code coverage. we are not covered below line in update operation.Can you help me on this where i made the mistake Task t = new Task(); t.WhatId = opp.Id; t.Subject = 'Help' + ': ' +opp.Help__c ; t.Description = opp.Description__c; t.ActivityDate = System.Today()+7; t.Status = 'In Progress'; t.Priority = 'Normal'; listTask.add(t);
    – Chinna
    Aug 23, 2016 at 7:51
  • I already outlined what needed done.
    – Eric
    Aug 23, 2016 at 8:08

1 Answer 1

3

testing is not about code coverage, it's about asserting it works as expected.

  • Trigger perform custom actions before or after changes to Salesforce records, so you need to check if a trigger do required action on a right records.
  • Positive and Negative scenarios should be tested.
  • All trigger should be bulk tested.

Lets take a closer look at this trigger:

  1. Trigger contexts are isInsert & isUpdate
  2. Condition for a trigger to work is to create a Task record on new Opp OR if Opp Help__c or Description__c is changed.

The main work for the trigger is to create Task records so this work should be asserted. Task always be created on insert, but on update it would create a Task only if special conditions are met => Negative scenario. With bulk testing in mind lets create a new Unit test:

  1. Insert 200 Opps then check that Trigger created 200 new Task records.
  2. Update 200 Opps, but change only 100 Help__c field, so we will have only 100 new Task records.

    static testMethod void TestInsertUpdate(){
    
        //Prepare data
        Account acc = new Account();            
        acc.Name='TestAcc';                                    
        insert acc; 
        //INSERT CONTEXT
    
        List<Opportunity> oppList = new List<Opportunity>();
        Integer numRepeats = 200;
        for (Integer idx = 0;  idx < numRepeats; idx++) {
            Opportunity Opp = new Opportunity();
            Opp.name = 'Sample Opportunity' + idx;
            Opp.AccountId = acc.id;
            Opp.Help__c ='Test';
            Opp.Description__c = 'Help' ;
            Opp.StageName = 'Prospecting';
            Opp.CloseDate = System.Today();  
            oppList.add(Opp);
        } 
    
        // Trigger entry point
        // we insert 200 records
        Insert oppList;  
        // we should assert we have 200 tasks created
        List<Task> testObjs = [SELECT Id, Description FROM Task];
        System.assertEquals(200, testObjs.size());
    
        // UPDATE CONTEXT
        // Change Help__c only for half Opps
        for (Integer idx = 0;  idx < numRepeats; idx++) {
            if (Math.mod(idx, 2) == 0) {
                oppList.get(idx).Help__c = 'New Value';
            }
        }
    
        update oppList;
    
        testObjs = [SELECT Id, Description FROM Task];
        // 100 new Task records + 200 from insert
        System.assertEquals(300, testObjs.size());
    }
    
4
  • 3
    not confident the OP will learn from a code dump.....
    – Eric
    Aug 23, 2016 at 8:09
  • 4
    Was supposed to outline that answers of pure code are not good resources. Some explanation of what the changes were and were the OP went wrong would be helpful
    – Eric
    Aug 23, 2016 at 8:14
  • You already outlined what needed done. If he can unite your description and my example maybe he can learn something. But reading docs must be the first step. Aug 23, 2016 at 8:21
  • 2
    Denis this got flagged, and as you can see you've already got -1 votes on it. I'd highly recommend adding some comments to explain why we use asserts etc. and I'm sure you'll see this turn into a positive score answer instead!
    – Matt Lacey
    Aug 25, 2016 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.