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
Ashish Kumar YadavAshish Kumar Yadav 

test class not able to cover

Hi Folks,

I am not able to cover below parts can anyone help me to cover this test class.code coverage is 72 percent.find below screenshot for your reference.

User-added imageUser-added image

Apex class
===========

global class BlockNonSaveReportExport implements TxnSecurity.EventCondition {
    //public string countId ;
    
    public boolean evaluate(SObject event){
        switch on event {
           
           when ReportEvent reportEvent {
                return evaluateReportEvent(reportEvent);
           }
           when null {
             //  System.debug('null');
               return true;
           } 
            when else {
              // System.debug('default');
               return true;
           } 
        }
    }
    
list<Id> ReportID= new list<Id>(); 
    
    public boolean evaluateReportEvent(ReportEvent reportEvent) {
        if (reportEvent.Operation == 'ReportExported' || reportEvent.Operation == 'ReportExportedAsynchronously' || reportEvent.Operation == 'ReportExportedUsingExcelConnector')
        {
            system.debug('reportEvent==='+ reportEvent);
            String strReportId = reportEvent.ReportId;
            string strsoql='SELECT Count() FROM Report Where Id =: strReportId ';
            Integer ReportRecordCount = Database.countQuery(strsoql);
            system.debug('ReportRecordCount=>'+ ReportRecordCount);
           
            if(ReportRecordCount == 0 ){
                return true;
            } 
            
            else if(ReportRecordCount!= 0 )
            { 
                system.debug('else===');
                return false;
            }    
        }
        return false;
}
  
}


Test Class
=========

@istest(SeeAllData=true)
public with sharing class BlockNonSaveReportExportTest {
   
    static testMethod void testReportEventPositiveTestCase() {
        Profile profileForSalesRep = [SELECT Id FROM Profile WHERE Name='PM RoW'];
      
        
        List<User> users = [SELECT Id,ProfileId FROM User where ProfileId =:profileForSalesRep.Id LIMIT 1];
        User user = (users.size() == 1) ? users.get(0) : null;
        
        
          // set up our event and its field values
          ReportEvent testEvent = new ReportEvent();
          testEvent.QueriedEntities = 'Accounts_Receivable__c';
          testEvent.ColumnHeaders='Customer Name';
          testEvent.Operation='ReportExported';
          testEvent.UserId =user.Id; 
          testEvent.ReportId='00ON00000019miVMAQ';
          system.debug('reportEvent==='+ testEvent);
        
     
          Test.startTest();
          System.runAs(user){
          BlockNonSaveReportExport  eventCondition = new BlockNonSaveReportExport();
            //  eventCondition.evaluateReportEvent(testEvent);
           //   eventCondition.evaluate(testEvent);
          System.assert(true,eventCondition.evaluateReportEvent(testEvent));
          System.assert(true,eventCondition.evaluate(testEvent));
          Test.stopTest();
            
       }
        
   }

    
  static testMethod void testReportEventPositiveTestCasesss() {
        Profile profileForSalesRep = [SELECT Id FROM Profile WHERE Name='PM RoW'];
      
        
        List<User> users = [SELECT Id,ProfileId FROM User where ProfileId =:profileForSalesRep.Id LIMIT 1];
        User user = (users.size() == 1) ? users.get(0) : null;
        
        
          // set up our event and its field values
          ReportEvent testEvents = new ReportEvent();
          testEvents.QueriedEntities = Null;
          testEvents.ColumnHeaders=Null;
          testEvents.Operation=Null;
         // testEvents.UserId =user.Id; 
          testEvents.ReportId=null;
          system.debug('reportEvent==='+ testEvents);
        
        
     Test.startTest();
          System.runAs(user){
          BlockNonSaveReportExport  eventCondition = new BlockNonSaveReportExport();
      //  eventCondition.evaluateReportEvent(testEvents);
          eventCondition.evaluate(testEvents);
          System.assertEquals(false,eventCondition.evaluateReportEvent(testEvents));
         //  System.assert(true,eventCondition.evaluate(testEvents));
          Test.stopTest();
            
       }
        
   }

    
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Ashish Kumar Yadav,

check that What Value you are expected null, may be reportEvent is not null then you can use System.assertEquals like this :
System.assertEquals(expected,actual);
Ex:
System.assertEquals(null, eventCondition.evaluate(testEvent)); 
if you have any query then please comment.

If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi