Day 19

This quick test is made to test your basic understanding of Apex Class Tests and importance of writing code tests.

  • You can look at the syntax, bonus points if you don’t.
  • The test should ideally not take more than 10 minutes to solve.
  • All the criterias must be met.
  • The answer is available at Admin2Dev GitHub Repo.
  • The answer and complete breakdown of the program is available at the end of this page.

The Test

Question

  • Write a batch class that, when executed, changes the First Name of contacts to Batch Test if the last name is Test and Batch Test 2 if the last name is Test 2.
  • Write a test class with 100% code coverage.

Start a timer and note how long you take to solve. Ideally, it should take ~10 minutes, but don’t worry if you go overboard.

Answer

Batch Class

global class NameChanger implements Database.Batchable<sObject> {
                global Database.QueryLocator start(Database.BatchableContext bc) {
                    // collect the batches of records or objects to be passed to execute

                    return Database.getQueryLocator('SELECT Id, LastName FROM Contact WHERE LastName = \'Test\' OR LastName = \'Test 2\' '); //Use the OR operator to get both Test and Test 2 values.
                }
                global void execute(Database.BatchableContext bc, List<Contact> conList){
                    //Process records

                    List<Contact> finalList = new List<Contact>();

                    for (Contact iterator: conList){
                        if (iterator.LastName == 'Test'){ //Check for lastName
                            iterator.FirstName = 'Batch Test';
                        }
                        else { //If lastName isn't Test, it's Test 2 because that's what the SOQL Query calls for
                            iterator.FirstName = 'Batch Test 2';
                        }

                        finalList.add(iterator);
                    }

                    update finalList;


                }
                global void finish(Database.BatchableContext bc){
                    // execute any post-processing operations
                }
            }
            

Test Class

@isTest
            public class NameChangerTest {

                @testSetup static void insertingData(){
            		 //Create test data
                    List<Contact> finalList = new List<Contact>();
                    for (Integer i = 1; i<=5; i++){
                        Contact con = new Contact();
                        con.lastName = 'Test';
                        finalList.add(con);
                    }
                    for (Integer i = 1; i<=5; i++){
                        Contact con = new Contact();
                        con.lastName = 'Test 2';
                        finalList.add(con);
                    }

                    insert finalList;
                }

                static testmethod void NameChangerTestMethod(){
                    //execute batch in test()
                    Test.startTest();
                    NameChanger tester = new NameChanger();
                    Id JobId = Database.executeBatch(tester);
                    Test.stopTest();

                    System.assertEquals(5, [SELECT count() FROM Contact WHERE LastName = 'Test' AND FirstName = 'Batch Test'], 'Failed Test Name Check');
                    System.assertEquals(5, [SELECT count() FROM Contact WHERE LastName = 'Test 2' AND FirstName = 'Batch Test 2'], 'Failed Test Name 2 Check');
                }

            }
            
Day 20: Scheduled Apex Class