-1

I've hit a wall. I cannot find a way to get my test coverage above 7/16 (43%)! I feel like I've tried everything, but nothing seems to make a difference, unless it causes my test crash. Can someone please add what I'm missing!!

Code Coverage Unit Test

@IsTest
public class PostWXtoChatterTEST
{
    static testMethod void PostWXtoChatterTEST()
    {
        Weather__c wx = new Weather__c();        
        wx.Air_Temp__c = 90.0;
        wx.Name = 'text';
        insert wx;
        FeedItem fitem = new FeedItem();
        fitem.parentID = '0F9J00000000DvF';
        fitem.body = 'Temporary Sample Text';
        insert fitem;
        System.assertEquals(wx.Air_Temp__c, 90.0);
        System.assertEquals(wx.Name, 'text');
        System.assertEquals(fitem.parentID, '0F9J00000000DvF');
        System.assertEquals(fitem.body, 'Temporary Sample Text');
    }
}
5
  • 3
    Prior to fixing your test class, I would recommend reviewing best practices for apex trigger development. You should never insert inside a for loop. Reference best practices here. developer.salesforce.com/page/Apex_Code_Best_Practices
    – Berg
    Dec 2 '15 at 18:30
  • Also please post the text of your code instead of images. It is far more accessible.
    – Adrian Larson
    Dec 2 '15 at 18:36
  • insert moved. Text version of my code added.
    – Rocket_Izz
    Dec 2 '15 at 18:44
  • 1
    and to add to the other good suggestions-- don't use hard coded IDs in testmethods - always mock the necessary data and then query for it
    – cropredy
    Dec 2 '15 at 19:10
  • Ok. I've done these things, but I still can't determine how to cover the red in my test. I've hard-coded, I've tried other ways too, but my 43% doesn't budge.
    – Rocket_Izz
    Dec 2 '15 at 19:11
6

Your test class can't see existing records unless you annotate with @isTest(SeeAllData=true) See salesforce doc. Even though the CollaborationGroup exists in your org, your test class can't access it by default. However, you should never use see all data unless you absolutely have too.

Instead, create your collaboration group in your test before you need it, like this:

@IsTest
public class PostWXtoChatterTEST
{
    static testMethod void PostWXtoChatterTEST()
    {

    //create collaboration group
    CollaborationGroup cg = new CollaborationGroup(Name = 'Elgin Weather Pulse', CollaborationType = 'Public');
    insert cg;

    Weather__c wx = new Weather__c();        
    wx.Air_Temp__c = 90.0;
    wx.Name = 'text';
    insert wx;
   ...

Then assert that a feed item was created

     List<FeedItem> fItems = [SELECT ID, ParentId FROM FeedItem WHERE ParentId = :wx.Id];
        System.assertEquals(1, fItems.size(), 'One feed item was created');
    }
}
3
  • (SeeAllData=true) pushed my code coverage up to 100%. This is the only thing I needed to add. Thank you.
    – Rocket_Izz
    Dec 2 '15 at 20:51
  • 3
    @Rocket_Izz - NO NO NO NO - You SHOULD NOT use see all data = true. The most important point of the answer seems to have not been read - MOCK All your data. Using see all data = true is BAD. When you used it you are able to see that your tests can pass not go the next step and update your test method to remove that and mock your data.....
    – Eric
    Dec 2 '15 at 21:24
  • Gotcha. I understand. I will make the adjustment. Thanks for taking time to correct me.
    – Rocket_Izz
    Dec 3 '15 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.