0

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);
    }
}
  • N.B. you should be able to work around the Survey mocking issue by adopting the Unit of Work pattern and ApexMocks – cropredy Nov 11 '19 at 19:39
1

You can easily remove the @seeAllData from the test class. Just take a look to this page

Test classes has @seAllData set to false as default. you can set in this way the setting to true only for interested methods.

The @isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, if the containing class has been annotated with @isTest(SeeAllData=true), annotating a method with @isTest(SeeAllData=false) is ignored for that method. In this case, that method still has access to all the data in the organization. Annotating a method with @isTest(SeeAllData=true) overrides, for that method, an @isTest(SeeAllData=false) annotation on the class.

  • Christian, welcome to SFSE. Link-only answers are discouraged because links go bad all the time. Don't remove the link, but please copy here the relevant parts of the article you linked so we can all see what you're suggesting. – Sebastian Kessel Nov 11 '19 at 16:11
  • @SebastianKessel, thanks for the advice, i've added further details – Christian Croce Nov 11 '19 at 16:19

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.