What is the optimal way to fix this?

@isTest
static void testIncrement() {
Account acct = new Account(Name = ‘Test’); acct.Number_Of_Times_Viewed__c = 0; insert acct;
AuditUtil.incrementViewed(acct.Id);
Account acctAfter = [SELECT Number_Of_Times_Viewed__c FROM Account WHERE Id = :acct.Id][0]
System.assertEquals(1, acctAfter.Number_Of_Times_Viewed__c);
}
The test method above calls an @future method that increments the Number_of_Times_Viewed__c value. The assertion is failing because the Number_of_Times_Viewed__c equals 0.
What is the optimal way to fix this?
A. Change the initialization to acct.Number_Of_Times_Viewed__c = 1.
B. Add Test.startTest() before and Test.stopTest() after AuditUtil.incrementViewed.
C. Add Test.startTest() before and Test.stopTest() after insert acct.
D. Change the assertion to System.assertEquals(0, acctAfter.Number_Of_Times_Viewed__c).

How To Pass Certified Platform Developer II Exam?

Certified Platform Developer II PDF dumps.

High quality Certified Platform Developer II pdf and software. VALID exam to help you pass.

Download Printable PDF. VALID exam to help you PASS.

3 thoughts on “What is the optimal way to fix this?

  1. This should be B

    Test.startTest and Test.stopTest are used for asynchronous apex, like Batch Apex and Future calls. Calling method(s) between Test.startTest and Test.stopTest ensure that any asynchronous transactions finish executing before Test.stopTest() exits. This means, that if you use System.asserts after Test.stopTest, you’ll have an accurate representation of your test. Otherwise, if you called System.assert before Test.stopTest, or if you didn’t use Test.startTest / Test.stopTest, then you couldn’t gaurantee that the batch or future transaction was complete.

    32

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.