+ Start a Discussion
MOHAMMED BABAMOHAMMED BABA 

Hello everyone, please help me to achieve 100% code coverage with the test class, i am very new to test classes and need help

Apex class ----
public without sharing class AnnouncementController    {
    @AuraEnabled(cacheable=true)
    public static List<Object_A> getNews(){
        List<user> user=  [Select Id,c_User_Country__c,ContactId,  isPortalEnabled from User where id= :UserInfo.getUserId()];
        System.debug('user->'+user);
        List<Object_A> newsRecords = new List<Object_A>();
        List<Object_A> nonDashboardRecords = new List<Object_A>();
        String userCountry =user[0].c_User_Country__c;
        String custType;
        Contact cont = getContact(UserInfo.getUserId());
        if (cont!=null){
            custType= cont.Account.Sub_Type__c;
            for(Object_A jc: [SELECT id,name,Title__c,Youtube_Video_Id__c, Image_URL__c, CreatedDate ,CreatedBy.Name,Is_Dashboard_Content__c  from Object_A where                           
                                    ((Start_Date__c <= TODAY AND End_Date__c >= TODAY) OR (Start_Date__c = null AND End_Date__c = null))
                                    AND (Country__c = :userCountry OR Country__c = 'Global') AND Customer_Type__c INCLUDES (:custType) AND Type__C='News' ORDER BY Sequence__c,CreatedDate ASC]){
                                        if(jc.Is_Dashboard_Content__c){
                                            newsRecords.add(jc);   
                                        }else{
                                            nonDashboardRecords.add(jc);  
                                        }    }
                                   
            newsRecords.addAll(nonDashboardRecords);
        }
        if (newsRecords==null || newsRecords.size()==0){
            userCountry = 'Other';
            if (cont==null){
                for(Object_A jc: [SELECT id,name,Title__c,Youtube_Video_Id__c, Image_URL__c, CreatedDate ,CreatedBy.Name,Is_Dashboard_Content__c  from Object_A where
                                        ((Start_Date__c <= TODAY AND End_Date__c >= TODAY) OR (Start_Date__c = null AND End_Date__c = null) )
                                        AND (Country__c = :userCountry OR Country__c = 'Global') AND Type__C='News' ORDER BY Sequence__c,CreatedDate ASC]){
                                            if(jc.Is_Dashboard_Content__c){
                                                newsRecords.add(jc);   
                                            }else{
                                                nonDashboardRecords.add(jc);  
                                            }     
                                        }
                newsRecords.addAll(nonDashboardRecords);
            }else{
                for(Object_A jc: [SELECT id,name,Title__c,Youtube_Video_Id__c, Image_URL__c, CreatedDate ,CreatedBy.Name,Is_Dashboard_Content__c  from Object_A where
                                        ((Start_Date__c <= TODAY AND End_Date__c >= TODAY) OR (Start_Date__c = null AND End_Date__c = null))
                                        AND (Country__c = :userCountry OR Country__c = 'Global') AND Customer_Type__c INCLUDES (:custType) AND Type__C='News' ORDER BY Sequence__c,CreatedDate ASC]){
                                            if(jc.Is_Dashboard_Content__c){
                                                newsRecords.add(jc);   
                                            }else{
                                                nonDashboardRecords.add(jc);  
                                            }     
                                        }
                newsRecords.addAll(nonDashboardRecords);
            }
        }
        system.debug('newsRecords :::'+ newsRecords);
        return newsRecords;
    }

 
AbhishekAbhishek (Salesforce Developers) 
Take the suggestions as mentioned in the below developer discussions,

Increase code coverage

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

75 %

https://salesforce.stackexchange.com/questions/24165/why-is-75-code-coverage-required-in-salesforce/24167#24167


If it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.