0

I am facing a strange scenario.

I have a method which I need to test.

There is a line in a method like this:-

if( items.size() > INVOICE_BATCH_SIZE ) {
    BillingInvoiceBatch b = new BillingInvoiceBatch(records);
    Database.executeBatch(b, INVOICE_BATCH_SIZE);
}

In my test class , I am creating 3 items and setting INVOICE_BATCH_SIZE to 2. And I want test coverage for this if block. So I want to start a batch. Problem is because execute batch has a scope mentioned i.e INVOICE_BATCH_SIZE , it will try to create two batches. If I create number of items equal to INVOICE_BATCH_SIZE it wont enter the if block.

How to test this if block ?

1
  • 1
    Just a though, but couldn't you just create two test methods? One for each condition, and then together they'd create the test coverage you want.
    – Kasper
    Sep 7, 2017 at 14:03

1 Answer 1

1

You can modify the if condition to as follows

if(items.size() > INVOICE_BATCH_SIZE || Test.isRunningTest()) {
    // conditional logic
}

This way you can bypass this condition from your test code.

4
  • Thanks for your reply but there has to be test coverage for the else part too and this will always enter the if part Sep 7, 2017 at 13:27
  • ok, can you please post the else part as well?
    – jagmohan
    Sep 7, 2017 at 13:28
  • nothing special there in the else part ... just updating some records .... Sep 7, 2017 at 13:52
  • Any particular reason for creating 3 items? Are you creating records at once in @TestSetup or in separate test methods?
    – jagmohan
    Sep 7, 2017 at 13:54

You must log in to answer this question.

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