I have written a test class where I need to insert Survey Invitation record, for which Survey required (i.e. Survey is parent and Survey invitation is child).
I'm querying the Survey record since Survey record is not possible to create from class. If (SeeAllData = True) then only Survey records will be retrieved. If (SeeAllData = False) query returns null.
So I'm using @isTest(SeeAllData = True) for a method, querying survey in the method and calling this method in test method
Here is my test class:
@isTest(SeeAllData = false)
private class CHS_Account_Survey_Invitation_TestClass {
private static Survey surRec = new Survey();
@isTest(SeeAllData = True)
private static void testWithSurvey(){
Survey surRec = [select id,Name from survey LIMIT 1];
system.debug('>>>surRec - 1: '+surRec);
}
private static testMethod void test(){
testWithSurvey();
system.debug('>>>surRec - 2: '+surRec);
Territory terrRec = [select id, Name from Territory LIMIT 1];
system.debug('>>>terrRec: '+terrRec);
/*SurveyInvitation surInvRec = new SurveyInvitation ();
surInvRec.OCE__Type__c = 'Account';
surInvRec.OCE__Territory__c = terrRec.Name;
//surInvRec.SurveyId = surRec.Id;
surInvRec.Name = 'Test Survey Invitation';
insert surInvRec;
system.debug('>>>surInvRec: '+surInvRec);*/
Account accRec = new Account();
accRec.FirstName = 'test class';
accRec.LastName = 'account';
accRec.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('MP (Medical Professional)').getRecordTypeId();
accRec.OCE__Territories__c = terrRec.Name;
insert accRec;
system.debug('>>>accRec: '+accRec);
OCE__AccountTerritoryFields__c atfRec = new OCE__AccountTerritoryFields__c();
atfRec.OCE__Account__c = accRec.Id;
atfRec.OCE__Territory__c = terrRec.Name;
insert atfRec;
system.debug('>>>atfRec: '+atfRec);
Test.startTest();
CHS_Account_Survey_Invitation_BatchClass obj = new CHS_Account_Survey_Invitation_BatchClass();
database.executeBatch(obj);
Test.stopTest();
}
}
Getting following error:
Class.CHS_Account_Survey_Invitation_TestClass.testWithSurvey: line 10, column 1 Class.CHS_Account_Survey_Invitation_TestClass.test: line 17, column 1
Is there any way to use SeeAllData = true inside only a method and not from the test class.
Here is my batch class:
global class CHS_Account_Survey_Invitation_BatchClass implements Database.Batchable<sObject>, schedulable {
global static List<String> terrNameSet = new List<String>();
global static List<SurveyInvitation> survInvList = [select id,OCE__Account__c, OCE__Territory__c, OCE__Type__c from SurveyInvitation WHERE OCE__Type__c = 'Account' AND OCE__Territory__c != NULL];
global static void setTerrName(){
for(SurveyInvitation surInvRec : survInvList){
terrNameSet.add(surInvRec.OCE__Territory__c);
}
}
global Database.QueryLocator start(Database.BatchableContext BC){
setTerrName();
string query;
if(Test.isRunningTest()){
query = 'select id,OCE__Account__c, OCE__Territory__c, CHS_No_of_Territory_Survey_Invitation__c from OCE__AccountTerritoryFields__c WHERE OCE__Account__r.IsPersonAccount =TRUE AND OCE__Territory__c != Null LIMIT 200';
system.debug('>>>Query :'+query);
}
else{
query = 'select id,OCE__Account__c, OCE__Territory__c, CHS_No_of_Territory_Survey_Invitation__c from OCE__AccountTerritoryFields__c WHERE OCE__Account__r.IsPersonAccount =TRUE AND OCE__Territory__c IN :terrNameSet ';
system.debug('>>>Query :'+query);
}
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<OCE__AccountTerritoryFields__c> scope){
system.debug('>>>scope :'+scope.size());
List<OCE__AccountTerritoryFields__c> updateAtfList = new List<OCE__AccountTerritoryFields__c>();
Map<String, List<SurveyInvitation>> terrSurInvMap = new Map<String, List<SurveyInvitation>>();
for(SurveyInvitation surInvRec : survInvList){
system.debug('>>>surInvRec: '+surInvRec);
if(surInvRec.OCE__Account__c == Null){
if(terrSurInvMap.containsKey(surInvRec.OCE__Territory__c)){
terrSurInvMap.get(surInvRec.OCE__Territory__c).add(surInvRec);
system.debug('>>>terrSurInvMap_1: '+surInvRec +terrSurInvMap.get(surInvRec.OCE__Territory__c));
}
else{
terrSurInvMap.put(surInvRec.OCE__Territory__c, new List<SurveyInvitation> {surInvRec});
system.debug('>>>terrSurInvMap_2: '+surInvRec +terrSurInvMap.get(surInvRec.OCE__Territory__c));
}
}
}
for(OCE__AccountTerritoryFields__c atfRec : scope){
system.debug('>>>atfRec: '+atfRec );
system.debug('>>>surInvCount: '+terrSurInvMap.get(atfRec.OCE__Territory__c));
SurveyInvitation[] inventory = terrSurInvMap.get(atfRec.OCE__Territory__c);
if(inventory != Null){
Integer surInvCount = inventory.size();
if( surInvCount > 0 && atfRec.CHS_No_of_Territory_Survey_Invitation__c != surInvCount){
atfRec.CHS_No_of_Territory_Survey_Invitation__c = surInvCount;
updateAtfList.add(atfRec);
}
}
}
try{
if(updateAtfList != Null && updateAtfList.size() > 0)
update updateAtfList;
}
catch(Exception ex){
system.debug('>>>Error : '+ex.getMessage());
}
}
global void finish(Database.BatchableContext BC){
}
global void execute(schedulableContext SC){
CHS_Account_Survey_Invitation_BatchClass schObj = new CHS_Account_Survey_Invitation_BatchClass();
database.executeBatch(schObj);
}
}
Survey
mocking issue by adopting the Unit of Work pattern and ApexMocks