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
Madevala JithenderMadevala Jithender 

How do i write test class for a Trigger

trigger ClosedOpportunityTriigger on Opportunity (after insert,after update) {
    list<task> tasklist = new list<Task>();
    
    for(opportunity opp : Trigger.new){
        if(opp.stagename == 'closed won'){
            tasklist.add(new task(subject = 'follow up Test Task',whatid = opp.id));
        }
    }if(tasklist.size()>0){
        insert tasklist;
    }

}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Madevala,

Can you use the below test class which gives you 100% coverage by using all the best pratices in test class.
 
@istest
public class ClosedOpportunityTriiggerTest {
   
 @testSetup static void insertopportunity(){
  List<Account> aList = new List<Account> {
       new Account (Name = 'My Test Account')
   };
  insert aList;
  List<Opportunity> oList = new List<Opportunity> {
     new Opportunity(Name ='opptest',
                        AccountID =aList[0].id,
                        StageName = 'Closed Won',
                        Amount = 3000,
                        CloseDate = System.today())
  };
  insert oList;
}

static testMethod void  testopportunity(){
    opportunity opp=[select id, name from opportunity where Name='opptest'];
  Task ts = [Select id,whatid,subject from Task where whatid =:opp.id ];
  system.assertEquals('follow up Test Task', ts.Subject );
}

}


Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
CharuDuttCharuDutt
Hii Madevala
Try Below Test Class 100% Coverage
@istest
public class ClosedOpportunityTriiggerTest {
   
 @testSetup static void insertopportunity(){
  List<Account> aList = new List<Account> {
       new Account (Name = 'My Test Account')
   };
  insert aList;
  List<Opportunity> oList = new List<Opportunity> {
     new Opportunity(Name ='opptest',
                        AccountID =aList[0].id,
                        StageName = 'Closed Won',
                        Amount = 3000,
                        CloseDate = System.today())
  };
  insert oList;
}

static testMethod void  testopportunity(){
    opportunity opp=[select id, name from opportunity where Name='opptest'];
  Task ts = [Select id,whatid,subject from Task where whatid =:opp.id ];
  system.assertEquals('follow up Test Task', ts.Subject );
}

}
Please Mark It As Best Answer If It Helps
Thank You!
Sai PraveenSai Praveen (Salesforce Developers) 
HI CharuDutt,

I see you are an active contributor on DF and appreciate all your valuable contribution. Offlate there have been a number of answers which are almost similar to mine which might not help end user.

Could you please refrain from posting similar content because in the end, it is only varied perspectives of solving problem that can add value to end users.

Same content is subject to deletion.

Thanks,