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
Maok kinesleMaok kinesle 

Hi I have tried code coverage for below test class But it covers only30% ,anyone please help me to make 80%

Original code:
public class MessageAlert {
    @auraenabled
    public static Boolean getmessages(String recordId){
        Boolean checkExpDate = False;
        List<LVSCR_c> contactList = new List<LVSCR_c>();
        Set<String> newSet = new Set<String>();
        //     Set<String> existingSet = new Set<String>();
        for ( LVSCR__c lvs : contactList ) {
            system.debug(lvs);
            if ( lvs.Contact__c != null ) {
                newSet.add(lvs.Contact__c);
            }
        }
        system.debug('newset--->>'+newSet);
        
        List <LVSCR_c> soql = new List<LVSCR_c>();
        soql = [SELECT Id,Contact_c FROM LVSCRc WHERE Id =: recordId AND Contact_c IN: newSet];
        system.debug('soql--->'+soql);
        if(soql.size()>0){
                //checkExpDate = True;
        }
        else{
                checkExpDate = False;
        }
        return checkExpDate;
    }
}

I hav tried but Its not covering the whole code.
apex test class:

@isTest
private class MessageAlertTest{
        @isTest
        static boolean getmessages(){
            List<LVSCR_c> conlist = new List<LVSCR_c>();
            Boolean checkExpDate = False;

            Lvscr_c lvscrcObj = new Lvscr_c ();
            lvscrcObj.Id='0012600001Ck4AEAA';
            lvscrcObj.Contact__c = '0012600001Ck4AEAA'; 
            lvscrcObj.Delivery_Type__c ='Original'; 
            lvscrcObj.LAFS__c = '122255'; 
            lvscrcObj.Tamm_Authorisation__c ='Yes';
            conlist.add(lvscrcObj);
            update conlist; 
            return checkExpDate;
      
         
    }

    @isTest static void testMethod1() {
        // code_block    
    }

    @isTest static void testMethod2() {
        // code_block    
    }
}
Suraj Tripathi 47Suraj Tripathi 47

Hi,

You can take references from this code.

@isTest
private class MessageAlertTest{
        @isTest
      public static void getmessagesTest(){
		Contact con=new Contact();
		con.lastName='Test Data';
		insert con;//insert all required field
		
		
            List<LVSCR_c> conlist = new List<LVSCR_c>();
            Boolean checkExpDate = False;

            LVSCR_c lvscrcObj = new LVSCR_c ();//insert all required field
			lvscrcObj.Name='Test';
            lvscrcObj.Contact__c =con.id;
            lvscrcObj.Delivery_Type__c ='Original'; 
            lvscrcObj.LAFS__c = '122255'; 
            lvscrcObj.Tamm_Authorisation__c ='Yes';
			
			
            LVSCR_c lvscrcObj1 = new LVSCR_c ();
			lvscrcObj1.Name='Test';
            lvscrcObj1.Contact__c =con.id;
            lvscrcObj1.Delivery_Type__c ='Original'; 
            lvscrcObj1.LAFS__c = '122255'; 
            lvscrcObj1.Tamm_Authorisation__c ='Yes';
            conlist.add(lvscrcObj);
			 conlist.add(lvscrcObj1);
			 
            insert conlist;
			
			test.startTest();
			MessageAlert.getmessages(conlist[0].Id);
			test.stoptest();
			
  
    }

    
}

Please mark it as the best Answer if your queries are solved.

Thank You