Skip to main content
13 réponses
  1. 24 oct. 2019, 10:33
    Hi,

    In Order to write a test class for batch class. 

    Please follow below steps for best Practices:

    1. Use @isTest at the Top for all the test classes

    2. Always put assert statements for negative and positive tests

    3. Use the @testSetup method to insert the test data into the Test class that will flow all over the test class.

    4. Always use Test.startTest() and Test.stopTest() doing this it will increase the governor limit of the salesforce. We also use this to increase the governor's limit.

    5. Use System.runAs() method to test the functionality in user Context.

    6. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.

    7. Avoid Using Hard Coding Ids anywhere in test Class or any apex class.

    8. Please make sure that each class has a minimum of 75% coverage and also the main functionality has been covered. If possible increase code coverage up to 95%.

    9. All class methods must be tested for at least 200 records and keep the real scenarios in mind.

    10.Only one Test.startTest() and Test.stopTest() statement can be in a method, and no of Test.startTest() and Test.stopTest() statement in any test class depend upon the test methods.

    Batch Class:

     

    global class Test_Class implements Database.Batchable<sObject>

    {

        global Database.QueryLocator start(Database.BatchableContext BC)

        {

            String query = 'SELECT Id,abc__c,CloseDate,StageName,Name FROM Opportunity';

            return Database.getQueryLocator(query);

        }

        global void execute(Database.BatchableContext bc, List<opportunity> scope)

        {

            List<opportunity>  ls=new list<opportunity>();

            Map<Id,opportunity> update_Map = new Map<Id,opportunity>();

            for(opportunity oppty:scope)

            {

                if(oppty.abc__c=='P' && oppty.abc__c!='M' 

                   && oppty.createdby.profile.Name=='Custom: TCT')

                {

                    oppty.abc__c='M';

                    //Updated below opptyListUpdate List to update_Map Map as to avoid duplicate values

                    update_Map.put(oppty.Id,oppty);

                    ls.add(oppty);

                    break;

                }

            }

        }

        global void finish(Database.BatchableContext BC) 

        {

        }

    }

    Test Class:

    @isTest

    public class Class_Test 

    {

        public TestMethod static void Class_Test_Method()

        {

            List<Opportunity> op_List = New List<Opportunity>();

            Opportunity op = new Opportunity();

            op.abc__c = 'P';

            op.name='Hello';

            op.CloseDate=date.today();

            op.StageName='Prospecting';

            op_List.add(op);

            Test.startTest();

            Database.SaveResult []str = Database.insert(op_List,false);

            system.assertEquals(True, str[0].isSuccess());

            Test_Class abcd = New Test_Class();

            Database.executeBatch(abcd);

            Test.stopTest();

        }

    }

    *It Shows 100% code coverage for this batch class

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks and Regards,

    Ajay Dubedi

    www.ajaydubedi.com
0/9000