0

How in the WORLD do I write a test class around this code? I've been able to get a successful test run, but the test says I have no code coverage. All this code does is take 5 text inputs passed in from Visualforce and post them to Chatter for all to see.

public with sharing class ChatterPostController{

    public String detail {get; set;}
    public String detail2 {get; set;}
    public String detail3 {get; set;}
    public String detail4 {get; set;}
    public String detail5 {get; set;}
    public Id groupId {get;set;}

    public PageReference doPost() {

        FeedItem fitem = new FeedItem();

        groupId = UserInfo.getUserId();
        fItem.parentId=groupId;
        fItem.body= 'Air Temperature: ' + detail + '\r\n' + 'Pavement Temperature: ' + detail2 + '\r\n' + 'Inches of Rain: ' + detail3 + '\r\n' + 'Inches of Snow: ' + detail4 + '\r\n' + 'Comment: ' + detail5;

        insert fItem;

        PageReference pageRef = ApexPages.currentPage();

        return null;
    }
}

Help is appreciated. Thank you.

  • 4
    Why don't you post what you have so far and we can help you move it forward. – JimRae May 13 '15 at 21:56
1

Its only the Apex (and not the Visualforce) that can be tested. So you invoke the class in your test and then assert what happens:

@isTest
private class ChatterPostControllerTest {

    @isTest
    static void test() {

        ChatterPostController c = new ChatterPostController();
        c.detail = 'd';
        c.detail2 = 'd2';
        c.detail3 = 'd3';
        c.detail4 = 'd4';
        c.detail5 = 'd5';
        c.doPost();

        FeedItem[] items = [select ParentId, Body from FeedItem];
        System.assertEquals(1, items.size());
        FeedItem item = items[0];
        System.assertEquals(UserInfo.getUserId(), item.ParentId);
        System.assertEquals(''
                + 'Air Temperature: ' + 'd' + '\r\n'
                + 'Pavement Temperature: ' + 'd2' + '\r\n'
                + 'Inches of Rain: ' + 'd3' + '\r\n'
                + 'Inches of Snow: ' + 'd4' + '\r\n'
                + 'Comment: ' + 'd5',
                item.Body
                );
    }
}
  • Thanks, Keith. It worked perfectly...and I was able to understand everything you did. My controller has now been moved from sandbox into production! – Rocket_Izz May 14 '15 at 19:19

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.