Skip to main content
Hi, All

I am new to salesforce, can anyone help me to get 100% coverage please 

public class MyContactListController 

{

    @AuraEnabled

    public static List<Contact> getContacts(Id recordId) 

    {

        List<Contact> conList = new List<Contact>();

        try{

            System.debug('--'+recordId);

            conList = [Select Id, FirstName, LastName, Email, Phone From Contact Where AccountId = :recordId];

            System.debug('conlist result----> '+conList);

        }

        catch(Exception e){

            System.debug('inside catch Block  -> '+e.getMessage());

        }

        return conList;

    }

}

-----------------------------------------

TestClass

-----------------------------------------

@isTest

public class MyContactListControllerTest {

   

    static testMethod void getContactsTest(){

        Account ac = new Account(Name='Name1');

        Insert ac;

        List<Account> acList = [select id, name from Account where id!=NULL];

        System.assertEquals(true,acList.size()>0);

        MyContactListController.getContacts(acList[0].id);

        List<Case> case_Obj  =  [SELECT Id,CaseNumber,AccountId From Case WHERE AccountId=:acList[0].Id LIMIT 10];

       

        MyContactListController.getContacts(acList[0].Id); 

      

    }

}
9 Antworten
  1. 6. Juni 2021, 16:25
    Hi Kristiana,

    Please use below code for 100% code coverage  without changes in your main class

     

    @isTest

    public class MyContactListControllerTest {

    static testMethod void getContactsTest_Try(){

    Account ac = new Account(Name='Name1');

    Insert ac;

    List<Account> acList = List<Account>();

    //System.assertEquals(true,acList.size()>0);

    Contact c=new Contact(

    AccountId = ac.Id,

    FirstName='fname',

    LastName = 'lname',

    Email = 'email@gmail.com',

    Phone = '9743800309');

    insert c;

    MyContactListController.getContacts(acList[0].id);

    //List<Case> case_Obj = [SELECT Id,CaseNumber,AccountId From Case WHERE AccountId=:acList[0].Id LIMIT 10];

    MyContactListController.getContacts(acList[0].Id);

    }

    static testMethod void getContactsTest_Catch(){

    Account ac = new Account(Name='Name1');

    Insert ac;

    List<Account> acList = List<Account>();

    //System.assertEquals(true,acList.size()>0);

    Contact c=new Contact(

    AccountId = ac.Id,

    FirstName='fname',

    LastName = 'lname',

    Email = 'email@gmail.com',

    Phone = '9743800309');

    insert c;

    MyContactListController.getContacts(acList[0].id);

    //List<Case> case_Obj = [SELECT Id,CaseNumber,AccountId From Case WHERE AccountId=:acList[0].Id LIMIT 10];

    List<String> ids = new List<String>();

    MyContactListController.getContacts(ids);

    }

    }

    if you need any assistanse, Please let me know!!

    Kindly mark my solution as the best answer if it helps you.

    Thanks

    Mukesh