0

I have a test class for a batch class that I can't get above 82% coverage. There is one part of the batch class that the test does not go into. I think it should be but it isn't. Any help is appreciated.

Batch class

public class BatchInvestigatorLastMonthSurvey  implements Database.Batchable<SObject> 
{
   public Database.QueryLocator start(Database.BatchableContext BC)

    {
        //query on Provider
        String query = 'Select Id,Last_Month_Quality_Surveys__c from Contact ';
        system.debug('return');
        return Database.getQueryLocator(query);

    }

    public void execute(Database.BatchableContext BC, List <Contact> spUpdate)
    {
         // Create LastMonthKey*******************************************************************************************
      // *****************************************************************************************************************
                       date myDate = date.today();
                     date lastDate = myDate.addMonths(-1);
                 Integer lastMonth = lastDate.Month();
                 Integer lastYear  = lastDate.Year();
               String lastMonthkey = string.valueof(lastMonth) + string.valueof(lastYear);   
      //************************************************************************************************************************


       Map<id,Contact>contactMap = new Map<id,Contact>(); 


       AggregateResult[] groupedResults =[Select count(id) noOfPro, provider__c from Survey_response__c where New_Quality_survey__c = true
                                           and Study__r.Master_Agreement_Study__c=True
                                            and Month_Year_Key__c =: lastMonthKey 
                                           and Study_Provider__r.Provider_Status__c != 'Terminated'
                                           group by provider__c ] ;  


       for(aggregateResult ar:groupedResults){

           Contact vStudyPro = new Contact();

           string accId=(string) ar.get('provider__c') ;
                decimal countofchild =(decimal)ar.get('noOfPro');
                vStudyPro=new Contact(id=accId,Last_Month_quality_surveys__c =countofchild);
                system.debug('Study Pro Id = '+ ar.get('provider__c'))  ;
                system.debug('Count of Surveys = '+ ar.get('noOfPro'));
                spUpdate.add(vStudyPro);

        }


    for (Contact studyProv:[Select Id, RecordTypeID
                  from  Contact
                  where RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Clinic')  
                            and Last_Month_Quality_Surveys__c != 0     
                            and Id not IN:spUpdate     ]){

        Contact theRest = new Contact();
        string restId=(string) studyProv.get('Id');
         decimal zeroz=0;                               
         theRest=new Contact(id=restId, Last_Month_quality_surveys__c = zeroz);                                 
        spUpdate.add(theRest);
                                     }

            for (Contact con:spUpdate){
            contactMap.put(con.Id,con);
        }                                 

            update contactMap.values(); 

        }



    public void finish(Database.BatchableContext BC)
    {

    }
}

and here is the test class:

@isTest (SeeAllData=false)
public with sharing class TestBatchInvestigatorLastQS{

    static testMethod void InvestigatorRecordTest() {

        Date todaysCurrentDate =date.today();
        date afterDrop= Date.newInstance(2016,12,02);
        date dropped = date.newInstance(2016,12,01);
        date beforedrop = date.newInstance(2016,11,05);
        date lastDate = todaysCurrentDate.addMonths(-1);

      string aStudy = 'a00A000000dIV3nIAG';

        List<RecordType> recordTypeList = [ SELECT ID, DeveloperName, Name, SobjectType FROM RecordType ];
        ID clinicRTid, patienRTid, accClinicRTid, pharmacyRTid;
        Id agreeFixFeeRTId, agreePPSurveyRTId;  // Agreement Record Types 
        Id agreeRuleFixFeeRTId, agreeRulePPSurveyRTId;  // Agreement Rule Record Types
        Id studyPartRTId, targetPartID;

        // Pick up the Record Type Id's for each of the Records we're going to create and test 
        for( RecordType aType : recordTypeList ) {
            if( aType.DeveloperName == 'Clinic' && aType.SobjectType == 'Contact' ) {
                clinicRTid = aType.ID;
            }
            if( aType.DeveloperName == 'Clinic' && aType.SobjectType == 'Account' ) {
                accClinicRTid = aType.ID;}
          if( aType.DeveloperName == 'Patient' && aType.SobjectType == 'Contact' ) {
          patienRTid = aType.ID;
            }

            if( aType.DeveloperName == 'Pharmacy' && aType.SobjectType == 'Account' ) {
                pharmacyRTid = aType.ID;
            }


            if( aType.DeveloperName == 'OPERA_Study_Participant' && aType.SobjectType == 'Study_Participant__c' ) {
                studyPartRTId = aType.ID;
            }   
            if(aType.DeveloperName == 'TARGET_Study_Participant' && aType.SobjectType == 'Study_Participant__c'){
                targetPartID =aType.ID;
            }

        }

        // create accounts
        Account anAcct = new Account();
        anAcct.Name = 'SGI Test';
        anAcct.RecordTypeID=accClinicRTid;
        anAcct.Account_Status__c='Active';
        anAcct.BillingCountryCode='US';
        anAcct.BillingStateCode='NY';
        anAcct.ShippingCountryCode='US';
        anAcct.Shipping_State__c='NY';
        insert anAcct;

        // create Pharmacy account
        Account aPharmacy = new Account();
        aPharmacy.Name = 'SGI Pharmacy Test';
        aPharmacy.RecordTypeID = pharmacyRTid;
         aPharmacy.BillingCountryCode='US';
       aPharmacy.BillingStateCode='NY';
       aPharmacy.ShippingCountryCode='US';
        aPharmacy.Shipping_State__c='NY';
        insert aPharmacy;

        // create Participant contact
        Contact pContact = new Contact();
        pContact.FirstName = 'T';
        pContact.LastName = 'C';
        pContact.RecordTypeID = patienRTid;
        pContact.Birthdate= date.newinstance(1977,12,01);
        pContact.Provider_Code__c = '52345';
        pContact.MailingCountryCode='US';
        pContact.MailingStateCode='NY';
        insert pContact;

        // create contact
        Contact aContact = new Contact();
        aContact.FirstName = 'Test';
        aContact.LastName = 'Contact';
        aContact.RecordTypeID = clinicRTid;
        aContact.Provider_Code__c = '12345';
       aContact.Date_Current_Agmt_Signed__c = todaysCurrentDate.addMonths(-3);
       aContact.Payment_Account__c = anAcct.Id;
        aContact.Provider_Clinic__c = anAcct.ID;
        aContact.Pharmacy__c = aPharmacy.ID;
        aContact.MailingCountryCode='US';
        aContact.MailingStateCode='NY';

        insert aContact;
     // system.debug(aContact);


        //system.debug(addContact);

        // create study
        Study__c bStudy = new Study__c();
        bStudy.Name = 'TARGET';
        bStudy.Minimum_Study_Age__c = 10;
        bStudy.Maximum_Study_Age__c = 90;
        bStudy.Start_Date__c = beforedrop;
        bStudy.Study_Prefix__c = 223;
        bStudy.Max_Study_Code__c = 54321;
        insert bStudy;


     //create study provider
        Study_Provider__c aProvider = new Study_Provider__c();
        aProvider.Provider__c = aContact.ID;
        aProvider.Provider_Clinic__c = anAcct.ID;
        aProvider.Pharmacy__c = aPharmacy.ID;
        aProvider.Provider_Status__c = 'Active';
        aProvider.Study__c = aStudy;
        aProvider.Study_Code__c = '123456';
      //  aProvider.Last_Month_Quality_Surveys__c = 20;
        aProvider.Payment_Account__c = anAcct.Id;

        insert aProvider;

       Survey__c s = new Survey__c();
        s.Name = 'Test Survey';
        s.Comments__c = 'Test Comments';
        s.Survey_Status__c ='Active';
        s.Survey_Amount__c = 10;
        s.Study__c=aStudy;
        s.Unqualified_Amount__c = 0;
        s.BaseLine_Survey__c=False; 
        insert s;

        Survey__c s2 = new Survey__c();
        s2.Name = 'Test Survey';
        s2.Comments__c = 'Test Comments';
        s2.Survey_Status__c ='Active';
        s2.Survey_Amount__c = 10;
        s2.Study__c=bStudy.id;
        s2.Unqualified_Amount__c = 0;
        s2.BaseLine_Survey__c=False;    
        insert s2;

    // Create Study Participant before CSE
        Study_Participant__c studyP = new Study_Participant__c();

        studyP.Name = 'Test Study Participant';
        studyP.HIPAA_Forms_Received__c=True;
        studyP.Patient_Authorization_Form__c =TRUE;
       // studyP.CSE_Forms_Received__c = TRUE;
        studyP.Participant__c= pContact.Id;
        studyP.Study_Provider__c=aProvider.Id;
        studyP.Provider__c = aContact.ID;
        studyP.Study__c = aStudy;
        studyP.Participant_Status__c = 'Active';
        studyP.RX_in_Florence__c = True;
        studyP.RecordTypeId = studyPartRTId;  
       // studyP.Date_of_Dropout__c = dropped;
        studyP.PK_Key__c='1659702884';

        insert studyP;


        // Create Study Participant after CSE
        Study_Participant__c studyP2 = new Study_Participant__c();

        studyP2.Name = 'After Study Participant';
        studyP2.HIPAA_Forms_Received__c=True;
        studyP2.Patient_Authorization_Form__c =TRUE;
        studyP2.CSE_Forms_Received__c = TRUE;
        studyP2.Participant__c= pContact.Id;
        studyP2.Study_Provider__c=aProvider.Id;
        studyP2.Provider__c = aContact.ID;
        studyP2.RX_in_Florence__c = True;
        studyP2.Study__c = bStudy.id;
        studyP2.Participant_Status__c = 'Active';
        studyP2.Insurance_Status__c = 'Covered';
        studyP2.RecordTypeId =  targetPartID;  
        studyP2.PK_Key__c='1659702884';     
        insert studyP2;

 Prescription__c Perp = new Prescription__c ();
        Perp.Study__c = aStudy; 
        Perp.Drug__c = 'Test Drugg'; 
        //Perp.NPI_PK__c = vNPI;
       // Perp.Participant_code__c = zName;
        Perp.Insurance__c = 'I';
        Perp.Study_Provider__c = aProvider.id;
        Perp.Study_Participant__c = studyP.id;
        Perp.Provider__c= aContact.id;
        Perp.Patient__c=pContact.id;
        Perp.Fill_Date__c = afterDrop;

        insert Perp;        

        //beforedrop and before CSE // New_Quality_Survey__c == True

        Survey_Response__c survey2 = new Survey_Response__c();

            survey2.Name='Test Survey Responses 1';
            survey2.Study__c = aStudy;
            survey2.Study_Participant__c = studyP.Id;
            survey2.Study_Provider__c=aProvider.Id;
            survey2.Survey__c=s.Id;
            survey2.Valid_Study_Participant__c = true;
            survey2.New_Quality_Survey__c=True;
            survey2.Source__c = 'eFax';
            survey2.Participant__c = pContact.Id;
            survey2.Payment_Status__c='New';
            survey2.Date_Time_Received__c = lastDate;
            survey2.Date_Taken__c=lastDate;
            survey2.Provider__c= aContact.Id;
            insert survey2;

        //no drop and after CSE //New_Quality_Survey__c = True
      Survey_Response__c survey6 = new Survey_Response__c();

            survey6.Name='Test Survey Responses 1';
            survey6.Study__c = bStudy.id;
            survey6.Study_Participant__c = studyP2.Id;
            survey6.Study_Provider__c=aProvider.Id;
            survey6.Survey__c=s2.Id;
            survey6.Valid_Study_Participant__c = true;
            survey6.Source__c = 'eFax';
            survey6.Participant__c = pContact.Id;
            survey6.Payment_Status__c='New';
            survey6.New_Quality_Survey__c=True;
            survey6.Date_Time_Received__c = lastDate;
            survey6.Date_Taken__c=lastDate;
            survey6.Provider__c= aContact.Id;
            insert survey6;  



      List<Survey_Response__c> surveyList = new List<Survey_Response__c>();
        Survey_Response__c survey5 = new Survey_Response__c();

         // Baseline received after droppedout // New_Quality_Survey__c ==False
        For (Integer i=0;i < 40;i++ ){
        survey5 = new Survey_Response__c();

            survey5.Name='Survey Response'+string.valueof(i);
            survey5.Study__c = aStudy;
            survey5.Study_Participant__c = studyP.Id;
            survey5.Study_Provider__c=aProvider.Id;
            survey5.Survey__c=s.Id;
            survey5.New_Quality_Survey__c=True;
            survey5.Valid_Study_Participant__c = true;
            survey5.Source__c = 'eFax';
            survey5.Participant__c = pContact.Id;
            survey5.Payment_Status__c='New';
            survey5.Date_Time_Received__c = lastDate;
            survey5.Date_Taken__c=lastDate;
            survey5.Provider__c= aContact.Id;
            surveyList.add(survey5);
        }
        insert surveyList;
       survey_response__c updateSurvey5 =[Select id, Quality_Survey_Override__c, Baseline_survey__c, Dropout_Date__c, New_Quality_Survey__c, Study__c, Study_Participant__r.Dropout_Date__c, 
                                          Study_Provider__r.Provider_Status__c from Survey_Response__c where id=:Survey5.id];
         update updateSurvey5;

        //the 'Not Quality' Override Survey // New_Quality_Survey__c ==False
        Survey_Response__c survey = new Survey_Response__c();
            survey.Name='Quality Test 2';
            survey.Study__c = aStudy;
            survey.Study_Participant__c = studyP.Id;
            survey.Study_Provider__c=aProvider.Id;
            survey.Survey__c=s.Id;
            survey.Valid_Study_Participant__c = true;
            survey.Source__c = 'eFax';
            survey.Participant__c = pContact.Id;
            survey.Payment_Status__c='New';
            survey.Date_Time_Received__c = lastDate;
            survey.Provider__c= aContact.Id;
            survey.Quality_Survey_Override__c = 'Not Quality';        
            insert survey;

        //the 'Quality' override Survey // New_Quality_Survey__c == True
        Survey_Response__c survey3 = new Survey_Response__c();
            survey3.Name='Quality Test 2';
            survey3.Study__c = aStudy;

            survey3.Study_Participant__c = studyP.Id;
            survey3.Study_Provider__c=aProvider.Id;
            survey3.Survey__c=s.Id;
            survey3.Valid_Study_Participant__c = true;
            survey3.Source__c = 'eFax';
            survey3.Participant__c = pContact.Id;
            survey3.Payment_Status__c='New';
            survey3.Date_Time_Received__c = lastDate;
            survey3.Provider__c= aContact.Id;
            survey3.Quality_Survey_Override__c = 'Quality';    
            insert survey3;


       contact addContact = new Contact();
        addContact.RecordTypeID = clinicRTid;
        addContact.FirstName='No';
        addContact.LastName='Study';
        addContact.Last_month_quality_surveys__c = 12;
        addContact.Provider_Code__c='33333';
        addContact.MailingCountryCode='US';
        addContact.MailingStateCode='NY';
        addContact.AccountId = anAcct.id;
        insert addContact;

    contact again = new Contact();
     again.RecordTypeID = clinicRTid;
        again.FirstName='Try';
        again.LastName='Again';
        again.Last_Month_Quality_Surveys__c= 0;
        again.Provider_Code__c ='43234';
        again.MailingCountryCode='US';
        again.MailingStateCode='TX';
        again.AccountId=anAcct.id;
        insert again;


        Test.startTest();


        contact UpdateaddContact=[Select Id, recordtypeid,last_month_quality_surveys__c from Contact where id=:addContact.id];
        update UpdateaddContact;

        contact testAgain=[Select Id, recordtypeid,Last_month_quality_surveys__c from Contact where id =:again.id];
        update testAgain;

        study_participant__c updateStudyP =[Select id, Date_enrolled__c, Dropout_Date__c, CSE_Forms_Received__c, Participant_Insurance_Status__c, RX_in_Florence__c from Study_Participant__c where id=:StudyP.id]; 
                    system.debug('StudyP= '+ updateStudyP);

         study_participant__c updateStudyP2 =[Select id, Date_enrolled__c, Dropout_Date__c, Participant_Insurance_Status__c, CSE_Forms_Received__c , RX_in_Florence__c from Study_Participant__c where id=:StudyP2.id]; 
                    system.debug('StudyP2= '+ updateStudyP2);


       BatchInvestigatorLastMonthSurvey myBatchObject1 = new BatchInvestigatorLastMonthSurvey();
        Database.executeBatch(myBatchObject1);



       Test.stopTest();

       Contact validateProviders = [select id, last_month_quality_surveys__c from contact  where id=:aContact.Id];
       update validateProviders;
      System.assertEquals(41,validateProviders.last_month_quality_surveys__c );   
      system.debug('last month qs for aContact = '+validateProviders.Last_month_quality_surveys__c);

      Contact testContactUp = [Select Id, recordtypeid, Last_month_quality_surveys__c from Contact  where id=:addContact.id];
       update testContactUp;  
       system.debug('Last Month # addContact '+testContactUp.last_month_Quality_Surveys__c) ;
       system.assertEquals(0,testContactUp.Last_Month_Quality_Surveys__c) ;

      contact testAgain2=[Select Id, recordtypeid,Last_month_quality_surveys__c from Contact where id =:again.id];
       system.assertEquals(0,testAgain2.Last_Month_Quality_Surveys__c) ;




    }

    }
2
  • If you indicate the lines that are not covered in your question that would make it easier for people to focus on the problem.
    – Keith C
    Commented Dec 8, 2016 at 20:50
  • This is the part with the problem. Contact theRest = new Contact(); string restId=(string) studyProv.get('Id'); decimal zeroz=0; theRest=new Contact(id=restId, Last_Month_quality_surveys__c = zeroz); spUpdate.add(theRest); Commented Dec 8, 2016 at 21:11

3 Answers 3

0

Additionally, please invoke "execute" method:

Database.executeBatch(myBatchObject1);
myBatchObject1.execute(null, new List<Contact>{yourContactsHere});

The same approach with "finish" method if needed.

4
  • I believe that will cause an error. Have you tried it yourself?
    – Adrian Larson
    Commented Dec 8, 2016 at 21:01
  • Yes I am using this approach very often. Please note that I have updated batch instance name from "batch" to "myBatchObject1" used in the question , now is clear I hope.
    – dan
    Commented Dec 8, 2016 at 21:05
  • 1
    Invoking Database.executeBatch between a Test.startTest(); and Test.stopTest(); will run the start/execute/finish cycle for one batch so is a good way to go for most batchables.
    – Keith C
    Commented Dec 8, 2016 at 21:15
  • the above looks like it will invoke the execute() method twice
    – cropredy
    Commented Dec 9, 2016 at 2:44
0

Create contact test data with the below record types. make sure the for loop SOQL where criteria is met.

Map <String,Schema.RecordTypeInfo> recordTypeMap = Contact.sObjectType.getDescribe().getRecordTypeInfosByName();

Contact contactObj = new Contact();
if(recordTypeMap.containsKey('Clinic')) 
{
 contactObj.RecordTypeId = recordTypeMap.get('Clinic').getRecordTypeId();
}

I think you need to make the below change too

@isTest (SeeAllData=true)
0

Your start() method return all contacts as there is no where condition. So spUpdate list in execute() will get all contacts returned from start(). In execute you were checking Id not IN : spUpdate in below for loop.

for (Contact studyProv:[Select Id, RecordTypeID
                  from  Contact
                  where RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Clinic')  
                  and Last_Month_Quality_Surveys__c != 0     
                  and Id not IN : spUpdate ])

Ideally there will be no contact because all the contacts you created is there in spUpdate. That's why code inside this for loop is not covered.

So please check your conditions while execute soql. Let me know your comments.

You must log in to answer this question.

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