0

I have a custom controller for which I've written a test class but getting an error when executing the Test -

Error Message System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Stack Trace Class.FollowedCasesPageTest.testPage: line 39, column 1

Controller -

public class CaseListViewController {

public List<Case> getEntity() {
    List<EntitySubscription> results = [Select ParentID from EntitySubscription where SubscriberId=:UserInfo.getUserId() and ParentId IN (Select Id from Case) ];

    set<ID> IDSet = new set<Id>();
for(EntitySubscription et: results ) {
  IDSet.add(et.ParentID );
}
    list<Case> caseList=[select Id,Subject,Status,CaseNumber,CreatedDate,   Sub_Status__c,Type from case where ID IN:IdSet];
    return CaseList;
    
}
    public List<Case> getCases() {
    List<Case> results1 = [Select Id,Subject,Status,Type,CreatedDate,   Sub_Status__c,  CaseNumber from Case where createdby.id=:UserInfo.getUserId()];
   return results1;
}
}

Test Class -

 @isTest 
private class FollowedCasesPageTest{
    
 @TestSetup
    static void setupData(){
        
        UserRole userrole = [Select Id, DeveloperName From UserRole  Limit 1];
        Profile sysadmin = [select id from profile where name='System Administrator']; 
        User adminUser =new User(UserRoleId =userrole.id,alias = 'standt', email='[email protected]',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = sysadmin.Id, timezonesidkey='America/Los_Angeles', username='[email protected]');
        insert adminUser;
        
        System.runAs(adminUser){
            Account a = new Account(Name='Test Account Name');
            insert a;
            
            Contact c = new Contact(LastName = 'Last Name', AccountId = a.id,email='[email protected]');
            insert c;
            
        }
        
        
        
    }
    @isTest static void testPage()
    { 
        Test.startTest();
     User u=[Select id,name from User where email='[email protected]'];
        System.runAs(u){
            
        
        
        Account a=[Select id from Account Limit 1];
        Contact c=[Select id from contact LIMIT 1];

         Case ca = new Case(Product_Categorisation__c='Mobile Application',Status='New',AccountId = a.id, ContactId = c.id, Subject='Test',origin='Phone',Type='Service Request',SuppliedEmail='[email protected]');
            insert ca;
             Case case1=[Select Id from case LIMIT 1];
        EntitySubscription es=new EntitySubscription(ParentId=case1.ID);
            insert es;
        
        
       
        EntitySubscription ess=[Select id from EntitySubscription];
        
        
    }
        Test.stopTest();
    }
}

1 Answer 1

1

You must also define the User who is subscribed to the EntitySubscription

EntitySubscription es = new EntitySubscription(
    ParentId = case1.Id,
    SubscriberId = u.Id);
insert es;

Of course, you can change the lookup reference to User in your test setup if you wish.

2
  • It worked like a charm, but the controller class is still showing test coverage as 0
    – newbie0007
    Commented Feb 9, 2022 at 11:46
  • 1
    I've passed the code coverage as well, thank you!
    – newbie0007
    Commented Feb 9, 2022 at 12:01

You must log in to answer this question.

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