function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chubsubchubsub 

Testing Batch Apex

How can I call the Contact records I'm creating in my test class?  How can I write a systeAssertEquals on the records that are inserted after the batch is ran?  Below is my test class, but it's throwing an error message that says the variable testcon does not exist when I try to query the contact that it is in the test class:

 

@istest
private class testBatchReferrals {

/*
Purpose: - This is the test method to test the batchReferrals class to ensure it runs properly
- Within this test class
- an account and 2 contacts are created.
- referral entry records are created based off the amount of similar Referral Physicians and MRN's there are in that month
- the batch process takes place
- the result will be 2 new referral entry summary records created that count the similar records for each month.


*/

static testmethod void testBatch(){
Test.startTest();

list<Account> accs = new list<Account>();
Account a1 = new Account(Name = 'test account1');
accs.add(a1);

list<Contact> testcon = new list<Contact>();
Contact c1 = new Contact(FirstName = 'test contact1',  AccountId = a1.Id);
Contact c2 = new Contact(FirstName = 'test contact2', AccountId = a1.Id);
testcon.add(c1);
testcon.add(c2);

list<Referral_Entry__c> testrefent = new list<Referral_Entry__c>();
Referral_Entry__c r1c1 = new Referral_Entry__c (Name = 'test referral 1 con 1', Referring_Physician_del__c = c1.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c r2c1 = new Referral_Entry__c (Name = 'test referral 2 con 1', Referring_Physician_del__c = c1.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c r1c2 = new Referral_Entry__c (Name = 'test referral 1 con 2', Referring_Physician_del__c = c2.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c r2c2 = new Referral_Entry__c (Name = 'test referral 2 con 2', Referring_Physician_del__c = c2.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c exsum = new Referral_Entry__c (Name = 'test summary referral 1 con 2', Referring_Physician_del__c = c2.Id, Service_Date__c = system.today(), Count_of_MRN__c = Decimal.Valueof('2'));
testrefent.add(r1c1);
testrefent.add(r2c1);
testrefent.add(r1c2);
testrefent.add(r2c2);
testrefent.add(exsum);


batchReferrals batch = new batchReferrals();
batch.contactquery = 'select Id from Contact where Id in :testcon';
ID jobId = Database.executeBatch(batch, 2);
Integer t = [Select count() from Referral_Entry__c where Count_of_MRN__c != Null Id in :testrefent];
system.AssertEquals(0, t);  - The size of this should be 6 (4 of the existing Referr_Entry__c records plus the 2 new summary Referral_Entry__c records.  The existing summary record listed above will be deleted since there is a value in the Count_of_MRN__c field.
Test.stopTest();

 

}

}

 

 

 

I'm getting 75% coverage, but I want to do some system asserts on the Referral Entry object to ensure there are actually 2 records created that have a value in the Count_of_MRN__c field once the batch is complete.

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

You can just hard code them in I think...though you never seem to actually insert the contacts in the example you give, but assuming that is an oversight...

 

Also - you need to query for the records created by the batch after the stoptest... 

 

So this example for querying for contacts works...

 

Account a1 = new Account (Name = 'testa');
                         insert a1;
                         
list<Contact> testcon = new list<Contact>();
Contact c1 = new Contact(LastName = 'test contact1',  AccountId = a1.Id);
Contact c2 = new Contact(LastName = 'test contact2', AccountId = a1.Id);
testcon.add(c1);
testcon.add(c2);
insert testcon;

String squery = 'Select Id, LastName from Contact where Id IN (\'' + c1.Id + '\',\'' + c2.Id + '\') ';
system.debug(squery);
List<Contact> testd = database.query(squery);
system.debug(testd);

 

All Answers

BritishBoyinDCBritishBoyinDC

You can just hard code them in I think...though you never seem to actually insert the contacts in the example you give, but assuming that is an oversight...

 

Also - you need to query for the records created by the batch after the stoptest... 

 

So this example for querying for contacts works...

 

Account a1 = new Account (Name = 'testa');
                         insert a1;
                         
list<Contact> testcon = new list<Contact>();
Contact c1 = new Contact(LastName = 'test contact1',  AccountId = a1.Id);
Contact c2 = new Contact(LastName = 'test contact2', AccountId = a1.Id);
testcon.add(c1);
testcon.add(c2);
insert testcon;

String squery = 'Select Id, LastName from Contact where Id IN (\'' + c1.Id + '\',\'' + c2.Id + '\') ';
system.debug(squery);
List<Contact> testd = database.query(squery);
system.debug(testd);

 

This was selected as the best answer
chubsubchubsub

Thanks BritishBoyinDC, that was a big oversight with not inserting the contact records I created in the test class.

 

I also took your advice on doing the system.Assert after the stoptest, but it still does not work correctly. 

 

The size of this should be 2 since the batch will delete one of the existing records with a value in the Count_of_MRN__c and create 2 records that will have values in that field. From looking at the debug log after querying the Referral_Entry__c records, it's still only listing the rows that were created before the batch took place.  

 

Integer t = [Select count() from Referral_Entry__c where Count_of_MRN__c != Null];
system.AssertEquals(1, t);

BritishBoyinDCBritishBoyinDC

Just to confirm - I don't see a line where you insert the testrefent list...?

 

You can post the batch code if that will help?

chubsubchubsub

Yeah, here it is.  The testrefent list is 5 rows like it should, but after the batch, one of those will be deleted (the one with a value in the Count_of_MRN__c field, and 2 more will be created with a value in this field.  These 2 new records created will have values in the Count_of_MRN__c field since it summarizes the records that were created for the same contact. 

 

 

 

static testmethod void testBatch(){
Test.startTest();

Account a1 = new Account(Name = 'testa');
insert a1;

list<Contact> testcon = new list<Contact>();
Contact c1 = new Contact(FirstName = 'test contact1', LastName = 'testLast', AccountId = a1.Id);
Contact c2 = new Contact(FirstName = 'test contact2', LastName = 'testLast', AccountId = a1.Id);
testcon.add(c1);
testcon.add(c2);
insert testcon;

list<Referral_Entry__c> testrefent = new list<Referral_Entry__c>();
Referral_Entry__c r1c1 = new Referral_Entry__c (Name = 'test referral 1 con 1', Sum_of_Charges__c = Decimal.Valueof('1'), FSC__c = 'None', Referring_Physician_del__c = c1.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c r2c1 = new Referral_Entry__c (Name = 'test referral 2 con 1', Sum_of_Charges__c = Decimal.Valueof('1'), FSC__c = 'None', Referring_Physician_del__c = c1.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c r1c2 = new Referral_Entry__c (Name = 'test referral 1 con 2', Sum_of_Charges__c = Decimal.Valueof('1'), FSC__c = 'None', Referring_Physician_del__c = c2.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c r2c2 = new Referral_Entry__c (Name = 'test referral 2 con 2', Sum_of_Charges__c = Decimal.Valueof('1'), FSC__c = 'None', Referring_Physician_del__c = c2.Id, Service_Date__c = system.today(), MRN__c = Decimal.Valueof('1'));
Referral_Entry__c exsum = new Referral_Entry__c (Name = 'test summary referral 1 con 2', Sum_of_Charges__c = Decimal.Valueof('1'), FSC__c = 'None', Referring_Physician_del__c = c2.Id, Service_Date__c = system.today(), Count_of_MRN__c = Decimal.Valueof('2'));
testrefent.add(r1c1);
testrefent.add(r2c1);
testrefent.add(r1c2);
testrefent.add(r2c2);
testrefent.add(exsum);


insert testrefent;
system.assertEquals(5, testrefent.size());

String squery = 'Select Id, LastName from Contact where Id IN (\'' + c1.Id + '\',\'' + c2.Id + '\') ';
system.debug(squery);
List<Contact> testd = database.query(squery);
system.debug(testd);
batchReferrals batch = new batchReferrals();
batch.contactquery = squery;
ID jobId = Database.executeBatch(batch, 2);
Test.stopTest();


Integer t = [Select count() from Referral_Entry__c where Count_of_MRN__c != Null];
system.AssertEquals(1, t);

BritishBoyinDCBritishBoyinDC

Not sure - looks right...

 

Try moving the Test.startTest(); to come after the insert testrefent; line...

chubsubchubsub

Thanks, I tried that, but it still didn't work.

 

I did notice something, I should have put in the Integer query to only query the Referral Entry records that include the Contact Id's I created in the test class, but that didn't work either, it's still counting 0 when it should be 2, below is the line I changed:

 

Integer t = [Select count() from Referral_Entry__c where Count_of_MRN__c != Null and Referring_Physician_del__c = :c1.Id and Referring_Physician_del__c = :c2.Id];
system.AssertEquals(0, t);

BritishBoyinDCBritishBoyinDC

Try changing this:

Integer t = [Select count() from Referral_Entry__c where Count_of_MRN__c != Null and Referring_Physician_del__c = :c1.Id and Referring_Physician_del__c = :c2.Id];
system.AssertEquals(0, t);

 

To be 

 

Integer t = [Select count() from Referral_Entry__c where Count_of_MRN__c != Null and Referring_Physician_del__c IN :testcon];

 


chubsubchubsub

Ahh, good suggestion.  It did change, it picked up one record instead of 0, but it still should have picked up 2 records.  I did check to see if it was picking up a new record, and it was, it's not reading the old record that was there since that one was deleted as it should be.  But, there still should be 2 records, I also tried to put the line before the Test.stopTest() line, but that didn't make a difference.  Thanks for you help with this, do you think that since it's testing a batch class, it will only test one at a time, even though I've specified 2?

chubsubchubsub

I found out that there was another trigger causing the confusion!