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 

Batch Apex Test Help - Testing Method called from Visualforce controller

I'm trying to get 100% coverage on by Apex Batch, so I had to remove this method and put it in it's own class, but I'm struggling as how to cover this method which calls the batch

 

//class to run batch from VS page


public static void runBatchJOB()
{
batchReferrals batchCls = new batchReferrals();
// 2 is size of batch
if (!test.isRunningTest())
database.executebatch(batchCls , 2);
}

 

 

static testmethod void testrunBatchJob()
{

 

//what can i put here to cover the runBatchJob()?

}

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

see my post in you other thread......

 

You need to remove the !test.isrunningtest if statement...

All Answers

kiranmutturukiranmutturu

Test.startTest(); // for TestCoverage, we have to use the Test.startTest() & Test.stopTest() methods
QuantityReturnBatchApex Obj = new QuantityReturnBatchApex();
Database.executeBatch(Obj);
Test.stopTest(); // Test.stopTest() method to stop the TestMethod

chubsubchubsub

Thanks Kiran, I should have been more clear, I've already tested the batch using the code below and got 94%.  The only code I could not test was this runBatchJob() method, which is the method called from my Visualforce page.  I was thinking, in order for me to cover this method, I would need to move it out of my batch class and into it's own class and only test that class.  So I did, I moved this runBatchJob() method to it's own class and I know have 100% on my batch class, but now I need to cover just this method. 

chubsubchubsub

This is what I've come up with so far, but it only has 7% coverage, I can't figure out how to cover the line in read below:

 

//class to run batch from VS page
public static void runBatchJOB()
{
batchReferrals batchCls = new batchReferrals();
// 2 is size of batch
if (!test.isRunningTest())
database.executebatch(batchCls , 2);
}

 

 

 

 

 

static testmethod void testrunBatchJob()
{
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;

String squery = 'Select Id, LastName from Contact where Id IN (\'' + c1.Id + '\',\'' + c2.Id + '\') ';
List<Contact> testd = database.query(squery);
runBatchReferrals rbr = new runBatchReferrals();
runBatchReferrals.runBatchJob();
batchReferrals batchCls = new batchReferrals();
ID jobId = Database.executeBatch(batchCls, 2);

}

Starz26Starz26

see my post in you other thread......

 

You need to remove the !test.isrunningtest if statement...

This was selected as the best answer