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
SFDC12SFDC12 

testclass for triggers

Hi everyone,below is the trigger and testclass which i have written.ablw to cover 70% can some one plz help me to increase the code coverage.
trigger:
trigger updateoppstage on Account (after update) {
set<id> accountId=new set<id>();
    
   
    for(Account a:trigger.new){
        accountId.add(a.id);
    }
    //query the account related opp
    List<opportunity>opp=new list<opportunity>();
   List<opportunity>opplist=[select id,name,stageName,createdDate,Accountid from opportunity where Accountid=:accountid];
    for(opportunity o:opplist){
        if(o.stageName!='ClosedWon'&&  o.CreatedDate<system.today()){
            o.stageName='ClosedLost';
        }
        opp.add(o);
    }
    update opp;
}
testclass:
@isTest
public class Testupdateoppstage {
@isTest
    static void call(){
        Account a=new Account();
        a.name='testacc';
         insert a;
        opportunity o=new opportunity();
        o.name='testopp';
        o.StageName='needanalysis';
        o.CloseDate=system.today();
        //o.CreatedDate=2-2-2021;
        o.AccountId=a.id;
       
        Test.startTest();
       List<opportunity>opp=[select id,accountId,stageName,name,closeDate,createdDate from opportunity where accountid=:a.id];
       
        update a;
         o.stageName='ClosedLost';
        update opp;
        //system.assertEquals(o.stageName, 'ClosedLost');
        Test.stopTest();
        
    }
}

Thanks in advance
Best Answer chosen by SFDC12
CharuDuttCharuDutt
Hii AnshiSFDC
Try Below Code 
@isTest
public class Testupdateoppstage {
@isTest
    static void UnitTest(){
        Account a = new Account();
        a.name='testacc';
         insert a;
        opportunity o = new opportunity();
        o.name='testopp';
        o.StageName='needanalysis';
        o.CloseDate=system.today();        
        o.AccountId=a.id;
        insert o;
		Test.setCreatedDate(o.id , DateTime.now().addDays(-1));
        Test.startTest();
       a.Name = 'testacc212';
        update a;
        Test.stopTest();
    }
}
Plase Mark It As Best Answer If It Helps
Thank You!
 

All Answers

Dushyant SonwarDushyant Sonwar
You can use Test.setCreatedDate method to set createdDate value in Opportunity object.
@isTest
public class Testupdateoppstage {
@isTest
    static void call(){
        Account a=new Account();
        a.name='testacc';
         insert a;
        opportunity o=new opportunity();
        o.name='testopp';
        o.StageName='needanalysis';
        o.CloseDate=system.today();        
        o.AccountId=a.id;
        insett o;
		Test.setCreatedDate(o.id , DateTime.now().addDays(-1));
        Test.startTest();
       List<opportunity>opp=[select id,accountId,stageName,name,closeDate,createdDate from opportunity where accountid=:a.id];
       
        update a;
         o.stageName='ClosedLost';
        update opp;
        //system.assertEquals(o.stageName, 'ClosedLost');
        Test.stopTest();
        
    }
}

 
CharuDuttCharuDutt
Hii AnshiSFDC
Try Below Code 
@isTest
public class Testupdateoppstage {
@isTest
    static void UnitTest(){
        Account a = new Account();
        a.name='testacc';
         insert a;
        opportunity o = new opportunity();
        o.name='testopp';
        o.StageName='needanalysis';
        o.CloseDate=system.today();        
        o.AccountId=a.id;
        insert o;
		Test.setCreatedDate(o.id , DateTime.now().addDays(-1));
        Test.startTest();
       a.Name = 'testacc212';
        update a;
        Test.stopTest();
    }
}
Plase Mark It As Best Answer If It Helps
Thank You!
 
This was selected as the best answer