1

I am writing a test class to test a class that generates a chatter post. I can get the code to compile however, when I run the test I keep getting:

List has no rows for assignment to SObject

I lifted some example code from Test Class - Existing Chatter Group - complication where the name can be dynamic however, I'm not getting the expected result.

Where am I going wrong?

Class:

public without sharing class NPD_ChatterPosts {

@TestVisible static String intelGroupName = 'Competitive Intelligence';
public static CollaborationGroup chatterGroup
{
  get
  {
    if (chatterGroup == null)
        chatterGroup = [SELECT Name, Id
                        FROM CollaborationGroup
                        WHERE Name = :intelGroupName
                        LIMIT 1];
        return chatterGroup;
    }
  private set;
}

public static void createCompetitiveIntelligencePost(List<Opportunity> chatFilter)
{       
  List<FeedItem> feedPost = new List<FeedItem>();

  // loop the Opportunities 
  for (Opportunity  oppy : chatfilter)
    {
        // create a chatter feed post alerting the team
        FeedItem fpost = new FeedItem();
        fpost.ParentId = chatterGroup.Id;
        fpost.LinkURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' +oppy.Id;
        fpost.Title = oppy.Name;
        fpost.Body = 'An Opportunity was flagged as "Closed Lost" to ' + oppy.Competition__c + ' please review.';
      feedPost.add(fpost);
      }
  insert feedPost;
}       

Test Class:

public with sharing class NPD_Test_ChatterPosts
{
static final String GROUP_NAME = 'NPD_UnitTest';
static Integer dummyIdCount = 0;
static final String OPP_PREFIX = SobjectType.Opportunity.getKeyPrefix();
static Id getDummyId()
{
    return OPP_PREFIX + String.valueOf(dummyIdCount++).leftPad(15, '0');
}


// test that we can get a chatter group
@isTest static void testChatterGroup()
{
    CollaborationGroup testGroup = new CollaborationGroup( Name = GROUP_NAME );
    NPD_ChatterPosts.intelGroupName = GROUP_NAME;

    Test.startTest();
        CollaborationGroup queryResult = NPD_ChatterPosts.chatterGroup;
    Test.stopTest();

    system.assertEquals(testGroup.Id, queryResult.Id,
        'The correct record should be queried');
}

// insert a feed post to the group
@isTest static void testFeedPost()
{
    List<Opportunity> o = new List<Opportunity>();
    Id tempId = getDummyId();

    o.add(new Opportunity(
        Id = tempId, 
        Name = 'Test'
    ));

    Test.startTest();
        NPD_ChatterPosts.createCompetitiveIntelligencePost(o);
    Test.stopTest();

    List<FeedItem> f = [SELECT Id FROM FeedItem];

    System.assertEquals(1, f.size());
}

1 Answer 1

2

Looks like you forgot to actually save the CollaborationGroup to the database:

CollaborationGroup testGroup = new CollaborationGroup( Name = GROUP_NAME );
insert testGroup;
NPD_ChatterPosts.intelGroupName = GROUP_NAME;

Whenever you need to query a record, you can't just create it in memory, you need to actually use a DML statement.

1
  • Doh, that was it. May 8, 2017 at 20:32

You must log in to answer this question.

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