0

I am trying to test a small scheduled apex class:

global class Vantage_Connector implements Schedulable {
   private Vantage_Messenger vm;

   public Vantage_Connector(Vantage_Messenger vm){
       this.vm = vm;
   }

   global void execute(SchedulableContext sch) {
    vm.postMessage();
   }   

}

this is the class I use to post the message:

global class Vantage_Messenger {
private ConnectApi.FeedElement post;
private VantageConnectorTestMessage__c vctm;

global void postMessage() {

    //get custom setting record
    //vctm = [Select Name,Message__c From VantageConnectorTestMessage__c];
    vctm = VantageConnectorTestMessage__c.getValues('Test2');

    //posts messege from VantageConnectorTestMessag
    if(vctm != null)
        System.debug('Vantage Connector Test Message in Vantage_Messenger__c -> '+vctm.Message__c);

    String mess = vctm.Message__c+' '+UserInfo.getOrganizationId();

    post = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), UserInfo.getUserId(), ConnectApi.FeedElementType.FeedItem, mess);

    System.debug('post id in VC ---->'+post.Id);
    System.debug('post body'+post.body);

}

 global String getTestMessage(){
    if(vctm != NULL) {
      return vctm.Message__c;
    } else {
      return NULL;
    }
}

  global String getPostId(){
    if(post != NULL){
      return post.Id;
    }else{
      return NULL;
    }
} 

}

When I run the test I see my debug statements accurately print the postID in the postMessage(). But after the test is ran postId is retrieved as null in the test class using the getPostId() method from Vantage_Messenger. See here:

 @isTest(SeeAllData=true)
 private class Vantage_Connector_Test {

   static testMethod void testConnectorTest() {
    //Schedule VCT will run after .stopTest()

   Test.startTest();

   String dummyCron = '0 2 9 ? * 2-6';

   Vantage_Messenger vm = new Vantage_Messenger();
   Vantage_Connector vct = new Vantage_Connector(vm);
   String jobId = System.schedule('Vantage Connector Test',dummyCron,vct);

   Test.stopTest();

//Test that message was posted to chatter
//should not be null
///check custom setting 

   String postId = vm.getPostId();
   System.debug('POST ID in TEST ---->'+postId);
   System.assertNotEquals(null,postId);

}

Post Id will be null in the log:

14:52:35:305 USER_DEBUG [23]|DEBUG|POST ID in TEST ---->null

and the test will fail.

What is going on ?

  • 1
    Try setting your test variables before the Test.startTest(): The startTest method marks the point in your test code when your test actually begins. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. – humanoid Jul 6 '18 at 23:17
2

I've experienced this kind of problem and my understanding is that that a copy (clone) of the objects that are scheduled (vm and vct in your case) is made and so the code executes on the copy not the original objects. And there is no way to access the fields of the copy.

(The code is going to run sometime in the future so the system needs to store the scheduled objects - serialize them - and then restore the scheduled objects - deserialize them - to run them.)

So you have to verify that the code under test has worked by checking a consequence of its actions in the test not the object itself. For your case you could (perhaps - I have not used the API you are using) query the feed item created.

PS

Ronnie's answer has a link to the recommended way to test that API.

2

It is poor practice to use seeAllData = true if you can avoid it. You would be best served to use a setup method to create some test data. Setup methods should be annotated with @TestSetup so that you can use reliable and consistently testable test data within your test classes. Also you can refer to the Documentation on testing ConnectApi for some help on writing your test cases for this API.

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.