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
chandrashekar Jangitichandrashekar Jangiti 

Hi Folks,I want to you guys to write Test class for below code, i did only 44% code coverage...

Hi Folks,I want to you guys to write Test class for below code, i did only 44% code coverage...
below is 
Apex class:
public with sharing class UpdatesOnOpptySave{
    public static void updateRecs(List<Opportunity> lstafterUpdateOppties){
        System.debug('Method invoked');
        for(Opportunity oppObj: lstafterUpdateOppties){
        if(oppObj.bizfin_pending__c == true &&(((oppObj.StageName == 'Shortlisted' || oppObj.StageName == 'Negotiation' ) && oppObj.Amount >= 1000000) || oppObj.CustomerType__c=='EE')) {
                oppObj.bizfin_pending__c = false;
                System.debug('----------------------------2nd If-------------------------'+oppObj.bizfin_pending__c );
            }
            
                      
            //If the opportunity stage is "Proposal Stage" and amount is less than 1 million uncheck pricing pending checkbox
            if((oppObj.StageName == Label.Opportunity_Phase_Proposal_Stage || oppObj.StageName == 'Shortlisted' || oppObj.StageName == 'Negotiation' ) && oppObj.Amount <1000000 ) {
                //Recall the opportunity from approval
                UpdatesOnOpptySave.recallOpportunity(oppObj.id);
                oppObj.Pricing_Approval_Pending__c = false;
            }
            
            if(oppObj.stagename==Label.Opportunity_Phase_Qualified_Lead && oppObj.Opportunity_Work_Type__c=='Staff Aug' && oppObj.Qualified_by_manager__c==false)
            {
                UpdatesOnOpptySave.recallOpportunity(oppObj.id);
                oppObj.Qualified_by_manager__c=true;
            }
    }
        
  } 
  
   public static void recallOpportunity(Id oppId){
        List<ProcessInstanceWorkitem> piwi = [SELECT Id, ProcessInstanceId, ProcessInstance.TargetObjectId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId =: oppId];
        
        if(!piwi.isEmpty()){
            
            Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
            req.setAction('Removed');        
            req.setWorkitemId(piwi.get(0).Id);
            req.setComments('Recalling the record as amount is less than 1 million');
            Approval.process(req,false);
           
            
        }
    } 
}

Test class:
@isTest
public class TestOppBeforeUpdate {
  @isTest static void TestUpdateOpportunity() {
       Profile p = [select id from Profile where profile.name='System Administrator' limit 1];

           User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test7778@gmail.com',
                             Alias='test',Email='test7778@gmail.com', TimeZoneSidKey='America/Los_Angeles', 
                             LocaleSidKey='en_US', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US');
            Insert u;
            System.runAs(u) {

      List<Opportunity> opplist=new List<Opportunity>();
            Account acc=new Account();
          acc.Name='test';
            insert acc;
      Contact con=new Contact();
      con.LastName='test';
      insert con;
      
Opportunity opp=new Opportunity();
      opp.Name='test';
      opp.StageName='test';
      opp.Type='test';
      opp.CloseDate=system.today()-3;
      opp.Description='test';
      opp.AccountId=acc.Id;
      opp.Account_Contact__c=con.Id;
      opp.Value_Proposition__c='test';
      opp.Contact_Role__c='test';
      opp.Start_Date__c=system.today()-5;
      opp.Winnability__c='test1';
      opp.Amount=1000;
      opp.BizFin_pending__c=false;
      opp.Qualified_by_Manager__c=true;
      opp.Pricing_Approval_Pending__c=false;
      opplist.add(opp);
          /*   Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitting request for approval.');
             req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
            req.setObjectId(opp.Id);
           Approval.ProcessResult resu = Approval.process(req);

           System.assert(resu.isSuccess());
           System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
           system.debug('status for approval::'+resu.getInstanceStatus());
            List<Id> newWorkItemIds = resu.getNewWorkitemIds();
            system.debug('newWorkItemIds ::'+newWorkItemIds );
                
                
            Approval.ProcessWorkitemRequest req2 =  new Approval.ProcessWorkitemRequest();
            req2.setComments('Approving request.');
            req2.setAction('Approve');
            req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
            system.debug('req2::'+req2);
           
           req2.setWorkitemId(newWorkItemIds.get(0));
            system.debug('req3::'+req2);
           Approval.ProcessResult result2 =  Approval.process(req2);           
            System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
            System.assertEquals(  'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
          ApexPages.currentPage().getParameters().put('id',opp.id);
            ApexPages.StandardController sc = new ApexPages.standardController(opp);
             UpdatesOnOpptySave rp = new  UpdatesOnOpptySave(sc);
            rp.appRej('Approve');
            rp.approveAll();
            rp.ApproveUnderCon();
            rp.getProsInsts();
            rp.rejectAll();

*/

      Test.startTest();
      UpdatesOnOpptySave.updateRecs(opplist);
      UpdatesOnOpptySave.recallOpportunity(opp.Id);
      Test.stopTest();
  }
  }
}


Thanks,
Chandu