0

Below is my method in the apex class and writing a test method for the same. When i am executing it from developer console, test class in not hitting the actual method..Can some one help me where i am failing, thanks in advance

Apex class:

public void  ChatterPost(Set<Id> setID){
    String feedBody='test';
    List <FeedItem > f= new List<FeedItem > ();

    for (Child__c objCann : [Select Name, id from Child__c where Id in: setId]){
        FeedItem feed = new FeedItem();
        feed.Body =feedBody;
        feed.ParentId  =objCann.Case__C;
        f.add(feed);
    }
    insert f;
}

Test Class:

static testMethod void testcannChatterPost(){
    String feedBody='';
    List <FeedItem > f =new List<FeedItem > ();
    List <Case> cse = new List<Case>();
    List <child__c> c = new List <child__c>();
    Case cs = new Case();
    ...
    ...
    cse.add(cs);
    insert cs;
    child__c c1 = new child__c(
    ...
    ...
    );
    c.add(c1);
    insert c;
    for(child__c c : c){
        FeedItem feed = new FeedItem();
        feed.Body = 'Test';
        feed.ParentId  =c.Case__C;
        f.add(feed);
    }
    insert f;

}
5
  • 2
    I don't see any call of your ChatterPost() method. But you didn't post your whole code so it's hard to help. Jul 26, 2017 at 7:20
  • we need not call the method in test class right? it should fire based on the criteria, if i am not wrong
    – venky
    Jul 26, 2017 at 7:22
  • I might be wrong, but i am not seeing any call to the method you are trying to test ChatterPost(), as far as i know, only the trigger code fires based on the DML operation.
    – user28829
    Jul 26, 2017 at 8:51
  • Unless your ChatterPost() method is being called from a trigger, then you do need to explicitly call your method. You haven't provided us with enough details to know if this is the case or not (it would be helpful if you were to edit your question to include that information).
    – Derek F
    Jul 26, 2017 at 13:21
  • That said, there are two things to keep in mind with testing: 1) You only get coverage for code that is run as part of the test. 2) If you aren't getting coverage, take a look at what conditions are required for that block to be run (did you insert records with the data to match the WHERE clause in a SOQL for loop? did you insert or pass the right data to satisfy that if() statement?). 3) Code coverage is always a secondary concern. Tests are useless without assertions. If you test enough (different) situations, coverage will follow. (Also, off-by-one errors are a beast).
    – Derek F
    Jul 26, 2017 at 13:23

1 Answer 1

1

As per my understanding from the code you provided, you are not calling the actual method: ChatterPost(); in your test class. After setting up the test data in your test class, you should be calling that. After that, you should consider using System.assert methods to test any possible outcomes.

System.assertNotEquals(Actual, TestData, '--Message--');
system.assertEquals(Actual, TestData, '--Message--');

Please refer to:

Salesforce documentation

Salesforce Stackexchange question

You must log in to answer this question.

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