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
Allen2Allen2 

I have written the below test class for the given below apex class, It is covering 82% now unable to cover some parts, Please anyone could halp me ...

public abstract class childController extends Controller {

    public Idea conIdea { get; set; }
    public String conCategories { 
        get { 
            if (conCategories == null && conIdea != null) { 
                conCategories = conIdea.categories; 
            }
            return conCategories;
        }
        set; 
    }    

    public String getVote() {
        return Math.Round(conIdea.vote).format();
    }


    public List<String> getSplitCategories() {
        if (conCategories == null) {
            return new String[0];
        } else {
            return conCategories.split(';');
        }
    }
    
    public String getCategory() {
        if (conCategories == null) return null;
        String[] splitCategories = getSplitCategories();
        return splitCategories[splitCategories.size() - 1];
    }

}

Test Class As....


@isTest
public class IdeaChildDetailsController extends IdeaDetailsAbstractController {

    static testMethod void testGetters() {
        IdeaTestUtil.setupTest();
        childController controller  =  new childController();
        Idea idea  =  IdeaTestUtil.makeIdea();
        controller.conIdea  =  idea;
        controller.conCategories  =  'sigma';
        controller.getSplitCategories();
        String getVote;
        getVote = '10';
        Integer myInt = Integer.valueOf(getVote);
        System.assertEquals(10, myInt);
        System.assert(controller.conCategories  !=  '');
    }
    static testMethod void testSplitCategories() {
        IdeaTestUtil.setupTest();
        childController controller  =  new childController();
        Idea idea  =  IdeaTestUtil.makeIdea();
        controller.conIdea  =  idea;
        controller.conCategories  =  null;
        controller.getSplitCategories();
        controller.getCategory();
    }
}

It is not covering the "
        String[] splitCategories = getSplitCategories();
        return splitCategories[splitCategories.size() - 1];"  this portion and "getVote"
Steven NsubugaSteven Nsubuga
@isTest
public class IdeaChildDetailsController extends IdeaDetailsAbstractController {

    static testMethod void testGetters() {
        IdeaTestUtil.setupTest();
        childController controller  =  new childController();
        Idea idea  =  IdeaTestUtil.makeIdea();
        controller.conIdea  =  idea;
        controller.conCategories  =  'sigma';
        controller.getSplitCategories();
        String getVote;
        getVote = '10';
        Integer myInt = Integer.valueOf(getVote);
        System.assertEquals(10, myInt);
        System.assert(controller.conCategories  !=  '');
    }
    static testMethod void testSplitCategories() {
        IdeaTestUtil.setupTest();
        childController controller  =  new childController();
        Idea idea  =  IdeaTestUtil.makeIdea();
        controller.conIdea  =  idea;
        controller.conCategories  =  null;
        controller.getSplitCategories();
        controller.getCategory();

        controller.conCategories  =  'sigma;gamma';
        System.assertEquals(2, controller.getSplitCategories().size());
        
        
        Idea idea  =  IdeaTestUtil.makeIdea();
        controller.conIdea  =  idea;
        System.assert(controller.getVote() != null);
    }
}

 
Allen2Allen2
Thanks Steven but still it is not covering these two portion.
 
Andy on CloudAndy on Cloud
When you test your controller.getCategory(); in your test method, are you sure it is not returning null? Because if it is returning null, which means for some reason the line "if (conCategories == null) return null;" is executed that conCategories is null so it returned null, that's why it's not covering the following two lines.

Try using assert to test the returning result from controller.getCategory() to see if it is null, then you can trace it from there.
Allen2Allen2
When it is returning null that time it covers "public String getCategory() {
        if (conCategories == null) return null;" this one and in 2nd method I m using the code u provide me where the null value not returning then it should work, but it is not and also this portion " public String getVote() {
        return Math.Round(conIdea.vote).format();"  not covering.
Andy on CloudAndy on Cloud
What I mean is that in your test method "testSplitCategories", the last line "controller.getCategory();", use assert to check the return value of this method.  If this method returns null, which means your below method return null, and if that is the case, you may have to trace it back to your test data setup:
public String conCategories { 
        get { 
            if (conCategories == null && conIdea != null) { 
                conCategories = conIdea.categories; 
            }
            return conCategories;
        }
        set; 
    }