+ Start a Discussion
NeelaNeela 

can someone help me with test class for below code?

public class UpdateOpportunity {
    public static void descriptionUpdate() {
        List<Opportunity> opp = [SELECT  Id,Name,Description From Opportunity WHERE StageName='Closed Won'limit 5];
        System.debug(opp);
        for ( Opportunity opps :opp)
        { 
            if(opps.Description == Null){
                opps.Description = 'This Opportunity is converted into Account';
            }
            else if(opps.Description == ''){
                opps.Description = 'This Opportunity is converted into Account';
            }
        }
              Update opp;
    }
}
Best Answer chosen by Neela
Suraj Tripathi 47Suraj Tripathi 47
Hi Neela,

You can take reference from the below test class:-
@isTest
Public class descriptionUpdateTest{
    @isTest public static void test(){
        List<opportunity> oppList= new List<opportunity>();
        for(integer i=0;i<5;i++){
            opportunity opp= new opportunity();
            opp.name='test '+i;
            opp.stageName='closed Won';
            opp.closedate=date.today();
            oppList.add(opp);
        }
        insert oppList;
        
        test.startTest();
        UpdateOpportunity.descriptionUpdate();
        test.stopTest();
    }
}

However, if you make these changes it will cover 100 %.
public class UpdateOpportunity {
    public static void descriptionUpdate() {
        List<Opportunity> opp = [SELECT  Id,Name,Description From Opportunity WHERE StageName='Closed Won'limit 5];
        System.debug(opp);
        for ( Opportunity opps :opp)
        { 
            if(opps.Description == Null || opps.Description == '' ){
                opps.Description = 'This Opportunity is converted into Account';
            }
        }
              Update opp;
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer.