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 ?