+ Start a Discussion
sumit dsumit d 

how to write negative test cases for a method

Hi All,
           I have a method in my class. How to write negative test cases for this method?
 public static void rollupForAttendee(){
        
        Set<Id> parentAttendeeIds = new Set<Id>();
        if( Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete ){
            for( rie__Log__c logObj : newLogs ){
                if( logObj.rie__Attendee__c != Null ){
                    parentAttendeeIds.add( logObj.rie__Attendee__c );
                }
            }
        } 
        else if( Trigger.isDelete ) {
            for( rie__Log__c logObj : oldLogs ){
                if( logObj.rie__Attendee__c != Null ){
                    parentAttendeeIds.add( logObj.rie__Attendee__c );
                }
            }    
        }
        
        if( parentAttendeeIds.size() > 0 ){
            
            for( Id registrationId : parentAttendeeIds ) {
                rie__Registration__c parentAttendee = new rie__Registration__c( Id = registrationId ); 
                parentAttendee.rie__Attendee_Counter__c = 0;
                parentAttendee.rie__Attendee_Chat_Counter__c = 0;
                parentAttendee.rie__Attendee_Connect_Counter__c = 0;
                mapParentAttendeesToUpdate.put( parentAttendee.Id, parentAttendee );
            }
            
            List<AggregateResult> aggrList = [ SELECT rie__Attendee__c parentAttendee, 
                                              COUNT(Id) attendee
                                              FROM rie__Log__c
                                              WHERE rie__Attendee__c IN: parentAttendeeIds
                                              GROUP BY rie__Attendee__c ];
            
            for( AggregateResult ag :  aggrList ){
                
                Id parentAttendeeId = (String)ag.get('parentAttendee');
                rie__Registration__c parentAttendee = mapParentAttendeesToUpdate.get( parentAttendeeId );
                
                parentAttendee.rie__Attendee_Counter__c = (Decimal)ag.get( 'attendee' );
                mapParentAttendeesToUpdate.put( parentAttendee.Id, parentAttendee );   
            }
            
            List<AggregateResult> aggrListChat = [ SELECT rie__Attendee__c parentAttendee, 
                                                  COUNT(Id) attendee
                                                  FROM rie__Log__c
                                                  WHERE rie__Attendee__c IN: parentAttendeeIds
                                                  AND rie__Attendee_Chat__c = TRUE
                                                  GROUP BY rie__Attendee__c ];
            
            for( AggregateResult ag :  aggrListChat ){
                
                Id parentAttendeeId = (String)ag.get('parentAttendee');
                rie__Registration__c parentAttendee = mapParentAttendeesToUpdate.get( parentAttendeeId );
                
                parentAttendee.rie__Attendee_Chat_Counter__c = (Decimal)ag.get( 'attendee' );
                mapParentAttendeesToUpdate.put( parentAttendee.Id, parentAttendee );   
            }
            
            List<AggregateResult> aggrListConnect = [ SELECT rie__Attendee__c parentAttendee, 
                                                     COUNT(Id) attendee
                                                     FROM rie__Log__c
                                                     WHERE rie__Attendee__c IN: parentAttendeeIds
                                                     AND rie__Attendee_Connect__c = TRUE
                                                     GROUP BY rie__Attendee__c ];
            
            for( AggregateResult ag :  aggrListConnect ){
                
                Id parentAttendeeId = (String)ag.get('parentAttendee');
                rie__Registration__c parentAttendee = mapParentAttendeesToUpdate.get( parentAttendeeId );
                
                parentAttendee.rie__Attendee_Connect_Counter__c = (Decimal)ag.get( 'attendee' );
                mapParentAttendeesToUpdate.put( parentAttendee.Id, parentAttendee );
            }
        }
    }
Can anyone help with negative test cases?
 
ShirishaShirisha (Salesforce Developers) 
Hi Sumit,

Greetings!

Can you please below document for the sample code for negative testing.

https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/negative-tests

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri