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
Andrew GrossAndrew Gross 

Newbie help with Unit Test

Hi Everyone.  I'm embarking on writing my first trigger/unit test and my test class is failing - not sure why.  If you can please provide some insight it would  be greatly appreciated.  Remember - take it easy on me I'm new to code writing =)

 

Scenario:  When a task is created with certain criteria (via user interface or in masse via data loader or whatever) - create a corresponding Opportunity with certain criteria.  That's basically it.

 

I'd like to know how I can dig deeper into what is wrong - does the debug log pick this stuff up?  I'm pretty sure the "test that the trigger worked" part of the test is screwy.  I've tried to take this as far as I can by looking up help files and looking at other posts on these boards. Thanks in advance.

 

Here's the trigger:

 

trigger insertNewDonation on Task (after insert, after update) {

List<Opportunity> listOppor = new List<Opportunity>();

for(Task t : Trigger.new)
{
if(t.Phonathon_Call_Result__c == 'Pledge' || t.Phonathon_Call_Result__c == 'Undecided')
  { 
  Opportunity o = new Opportunity();
  
  o.Amount = t.Pledge__c;
  o.CloseDate = t.ActivityDate;
  o.Name = 'Overwrite';
  o.Donation_Category__c = 'Phonathon';
  o.Contact_Email__c = t.WhoId;
  o.npe01__Contact_Id_for_Role__c = t.WhoId;
  o.AccountId = t.AccountId;
  
  if(t.Phonathon_Call_Result__c == 'Pledge'){
  o.StageName = 'Pledged';}
  else{
  o.StageName = 'Prospecting';
  }
  
listOppor.add(o);
  }
}

if(listOppor.size() > 0)
     try {
        insert listOppor; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }  

 Here's the test Apex Class

 

@isTest 
private class TestTaskDonationInsert {
    static testMethod void testDonationInsert() {
       
       // Prep Data
       List<Task> tasks = new List<Task>();
       for(Integer i = 0;  i < 100; i++){
           
           Task t = new Task(Subject= 'Phonathon Call' +i,Phonathon_Call_Result__c = 'Pledge', ActivityDate = date.today(), Pledge__c=100.0);
           tasks.add(t);
           
        Test.startTest();
        
       // DML Operation
        insert tasks;
        
        Test.stopTest();
           

       // Verify Data
       List<Opportunity> insertedOpps = [SELECT Amount, StageName, Donation_Category__c
                                   FROM Opportunity
                                   WHERE StageName = 'Pledged'];
    
     
       // Test that the trigger worked
       System.assertEquals('Pledged', insertedOpps[0].StageName);
    }
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Andrew GrossAndrew Gross

I ended up moving up the } to close my for loop before starting system testing and that seemed to work.  Thanks for the help!

All Answers

Devendra@SFDCDevendra@SFDC
Hi Andrew,

Is there any specific error you are facing in test above test method?

Thanks,
Devendra
Andrew GrossAndrew Gross

I just found where I can get more detail .....it compiles but this is why the class fails:

 

Method Name	testDonationInsert
Pass/Fail	Fail
Error Message	System.FinalException: Testing already started
Stack Trace	(System Code)
Class.TestTaskDonationInsert.testDonationInsert: line 12, column 1

 

 

Andrew GrossAndrew Gross

I ended up moving up the } to close my for loop before starting system testing and that seemed to work.  Thanks for the help!

This was selected as the best answer
Devendra@SFDCDevendra@SFDC
Hi Andrew,

Glad you fixed this issue. Very well done!!

Thanks,
Devendra