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
Mona KawaleMona Kawale 

System.AssertException: Assertion Failed: Expected: 1, Actual: 0

Trigger 
trigger ContactttTrigger on Contact (after insert, after update) {
     if(trigger.isinsert || trigger.isupdate && trigger.isafter){
        ContactHandler.mymethod(trigger.new);
    }

}

Handler Class
public class ContactHandler {
    
    public static void mymethod(list<contact> conlist){
        list<Contact_Relationship__c> contlist=new list<Contact_Relationship__c>();
        for(contact con:conlist){
                        
            if(con.Contact_Relationship__c==true){
                Contact_Relationship__c CR=new Contact_Relationship__c();
                CR.name=con.lastname;
                cr.Contact__c=con.id;
                contlist.add(cr);
            }
        }
        if(!contlist.isempty()){
            insert contlist;}
        
    }
}

Test Class
@istest
public class ContacttttTest {
    @istest
    public static void testme(){
        
        Contact con=new contact();
        con.lastname='Jack';
        con.Contact_Relationship__c=true;
        insert con;
        
        Contact con1=new contact();
        con1.lastname='Ma';
        con1.Contact_Relationship__c=false;
        insert con1;
        
        
        list<contact> conlist=[select id, Lastname, Contact_Relationship__c from contact];
        Test.startTest();
        ContactHandler.mymethod(conlist);
        Test.stopTest();
        System.assertEquals(1, [select count() from Contact_Relationship__c where name='Mona']);
    }
    
}


 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The error is expected because only for first contact you are giving Contact_Relationship__c as true so one contact will be created with name as Jack. You have to check the query with that where condition.
 
Test Class
@istest
public class ContacttttTest {
    @istest
    public static void testme(){
        
        Contact con=new contact();
        con.lastname='Jack';
        con.Contact_Relationship__c=true;
        insert con;
        
        Contact con1=new contact();
        con1.lastname='Ma';
        con1.Contact_Relationship__c=false;
        insert con1;
        
        
        list<contact> conlist=[select id, Lastname, Contact_Relationship__c from contact];
        Test.startTest();
        ContactHandler.mymethod(conlist);
        Test.stopTest();
        System.assertEquals(1, [select count() from Contact_Relationship__c where name='Jack']);
    }
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,