0

I was wondering if you can help me with my test script. My Code Coverage Failure is 74% and I need to get it up to 75% or above.

==================== myController =====================

public with sharing class MyBadgeController {

    public  List<myBadgeSummary> Summaries { get; set; }
    public String id{get;set;}
    //public String bId{get;set;}
    public Decimal mytotalBadges{get;set;}
    public Decimal mytotalChatterFeed{get;set;}
    private User user;
    private List<Internal_Badge__c> myBadgeList;

    private List<FeedItem> myChartterFeedList ;
    public Internal_Badge__c singleBadge {get; set;}

    //Internal_Badge__c singleBadge = null;

    public User getUser() {
        user = [SELECT id, email,FullPhotoUrl, username, usertype, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email,Aboutme
                FROM User
                WHERE id = :UserInfo.getUserId()];
        return user;
    }

    public List<Internal_Badge__c> getMyBadgeList() {

        myBadgeList = [SELECT Id, ResourceName__c,Badge_Comments__c, Badge_Image__c, Badge_Name__c, 
                 Given_By__c, Given_To__c, User__c,CreatedDate,Total_Badges__c
                 FROM Internal_Badge__c WHERE User__c = :UserInfo.getUserId() ];
        mytotalBadges  = 0;
        Summaries = new List<myBadgeSummary>();
        for (Internal_Badge__c ar : myBadgeList ) {
            mytotalBadges = mytotalBadges +1;

            Summaries.add(new myBadgeSummary(ar));
        }


        return myBadgeList;

    }

    public Decimal mytotalChatterFeed() {
        myChartterFeedList = [SELECT Id FROM FeedItem  WHERE CreatedById = :UserInfo.getUserId() ];
        mytotalChatterFeed = 0; 
        for (FeedItem  fitem: myChartterFeedList ) {
            mytotalChatterFeed = mytotalChatterFeed +1;   
        }  

        return mytotalChatterFeed ;
    }

    public MyBadgeController() {

       getUser();               
       getMyBadgeList();
       mytotalChatterFeed();         

    }

     // wrapper class to hold aggregate data
    public class myBadgeSummary {
        public string Id{ get; private set; }
        public Integer TotalBadges { get; private set; }
        public String ResourceName{ get; private set; }
        public String BadgeComments{ get; private set; }
        public String GivenBy{ get; private set; }
        public String GivenTo{ get; private set; }
        public Datetime  CreatedDate{ get; private set; }
        public String BadgeName {get; private set;}
        public myBadgeSummary (Internal_Badge__c ar) {

            Id= (string ) ar.get('Id');
            TotalBadges = (Integer) ar.get('Total_Badges__c');
            ResourceName = (String) ar.get('ResourceName__c');
            CreatedDate = (Datetime) ar.get('CreatedDate');
            BadgeComments = (String) ar.get('Badge_Comments__c');
            GivenBy= (String) ar.get('Given_By__c');
            GivenTo= (String) ar.get('Given_To__c');
            BadgeName =(String) ar.get('Badge_Name__c');
        }
    }
}

=================================

Test Script

@isTest
public class thecontrollerTests {

    public static testMethod void testMyBadgeController() {
        //PageReference pageRef = Page.MyBadgePage;
        //Test.setCurrentPage(pageRef);
        PageReference pageRef = Page.MyBadgePage;
        Test.setCurrentPage(pageRef);

        MyBadgeController controller = new MyBadgeController ();

        // Instantiate a new controller with all parameters in the page
        controller = new MyBadgeController (); 

        //test Badges
        List<Internal_Badge__c> msgList = controller.getMyBadgeList();
        System.assertNotEquals(null, msgList);

        //test Users list
        User user = controller.getUser();
        System.assertNotEquals(null, user );

         //test totalfeed count
        Decimal totalfeed = controller.mytotalChatterFeed();       
        System.assertNotEquals(null, totalfeed);
    }
}
3
  • could you please make screenshot showing which line are not covered?
    – Patlatus
    Dec 14, 2016 at 12:30
  • 1
    If you use Developer console that will allow you to grab the screenshot of the lines covered.
    – Dave Humm
    Dec 14, 2016 at 14:25
  • @Patlatus All of the following is not covered. thanks public class myBadgeSummary { public string Id{ get; private set; } public myBadgeSummary (Internal_Badge__c ar) { Id= (string ) ar.get('Id'); TotalBadges = (Integer) ar.get('Total_Badges__c'); ResourceName = (String) ar.get('ResourceName__c'); CreatedDate = (Datetime) ar.get('CreatedDate'); BadgeComments = (String) ar.get('Badge_Comments__c'); BadgeName =(String) ar.get('Badge_Name__c'); } }
    – zaktecs
    Dec 16, 2016 at 12:39

2 Answers 2

2

From what I can see of what you've posted, your test class isn't creating a RunAs user nor is it creating the badges and associating them with that user. Consequently, when your test class runs, it's dependent on the User who runs the test class and any badges that user happens to have associated with them.

Your class also has the following public variables as Apex properties:

public List<myBadgeSummary> Summaries { get; set; }
public String id{get;set;}
public Decimal mytotalBadges{get;set;}
public Decimal mytotalChatterFeed{get;set;}
public Internal_Badge__c singleBadge {get; set;}

Unless you have these available in your test class, your controller can't "get" them to later "set" them. You may be calling a new instance of the visualforce page, but the page's variables won't be populated unless they're included in the class.

5
  • Hi crmprogdev and Patlatus,
    – zaktecs
    Dec 16, 2016 at 11:56
  • Hi crmprogdev and Patlatus, Thanks for your comments and recommendation. when I do unit test in developer console I get 100% passed but my custom controller is still 0% coverage. I think my test is not hitting my control. How can I initiate the controller call the methods public MyBadgeController() { getUser(); getMyBadgeList(); mytotalChatterFeed(); }Many thanks
    – zaktecs
    Dec 16, 2016 at 12:04
  • @zaktecs You get 100% passed on what? Is this supposed to be a Controller Extension? What object is the page written for? Is it on Internal_Badge__c? You have a method that's named the same as what should be your constructor, but it isn't located what your constructor should be located MyBadgeController() & also don't declare a new instance of the standardcontroller. I think the issue is with the way you've written your class. See the VF Reference Guide on writing Extensions & Custom Controllers.
    – crmprogdev
    Dec 16, 2016 at 13:02
  • thanks for your comments and direction. I have managed to resolve this issue by seperating my myBadgeSummary to a seperate class.
    – zaktecs
    Dec 19, 2016 at 14:41
  • It sounds as though your issue was as much with the code you were testing as with your test method if you created a separate class. If my comments and direction told you what you needed to resolve your issue, posting your final code and marking it as the answer to your question isn't the way this forum works. Had I not posted my answer, you wouldn't be posting your code that "worked". I suggest you read Accepted Answer in the Help center.
    – crmprogdev
    Dec 19, 2016 at 15:55
-1

Here is the test that worked for me.

@isTest
public class tstMyBadgePage {

 static testMethod void myUnitTest() {
        test.startTest();       

         Decimal mytotalBadges = 0;

    List<myBadgeSummary> Summaries ;
    myBadgeSummary stack = new myBadgeSummary();

    PageReference testPage = new pagereference('/apex/MyBadgePage');

     MyBadgeController MyControllerObj = new MyBadgeController();

         Internal_Badge__c newBadge = new Internal_Badge__c (Badge_Comments__c='Badge Comments',Badge_Name__c='Thank You');
         Insert newBadge;

      List<Internal_Badge__c> myBAdges = [SELECT Id, ResourceName__c,Badge_Comments__c, Badge_Image__c, Badge_Name__c, 
                 Given_By__c, Given_To__c, User__c,CreatedDate,Total_Badges__c
                 FROM Internal_Badge__c  where Id=:newBadge.Id ];
        List<Internal_Badge__c> msgList = MyControllerObj.getMyBadgeList();
        System.assertNotEquals(null, msgList);

        mytotalBadges  = 0;
        Summaries = new List<myBadgeSummary>();
        for (Internal_Badge__c ar : myBAdges ) {
            mytotalBadges = mytotalBadges +1;           
            Summaries.add(new myBadgeSummary(ar));
            System.assertEquals(ar.Badge_Name__c, newBadge.Badge_Name__c);
        }

        System.assertNotEquals(null, Summaries);
        //test Users list
        User user = MyControllerObj.getUser();
        System.assertNotEquals(null, user );

         //test totalfeed count
        Decimal totalfeed = MyControllerObj.mytotalChatterFeed(); 
        totalfeed = 3;
        System.assertNotEquals(null, totalfeed);

        Decimal mytotalBadges1 = MyControllerObj.mytotalBadges;  
        mytotalBadges = 1;
        System.assertNotEquals(null, mytotalBadges);

        Decimal mytotalChatterFeed = MyControllerObj.mytotalChatterFeed;  
        System.assertNotEquals(null, mytotalChatterFeed);

        test.stopTest();
}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .