Apex Unit Test – How to Assert That a Map Has the Needed Values

if anyone could help; I'd appreciate some guidance on a test class I am working on. I am new to Apex and I am trying to test a class that uses a map which is the only piece of code I need testing for.

I am receiving 3 errors which are likely to surround the same issue but am not quite sure what I am doing wrong.

Error:Expecting '}' but was: 'System.assertEquals'
Error:Unreachable statement
Error:Extra '', at '}'.

@isTest
private inherited sharing class FLLC_Foreman_Job_Packet_Helper_Test
{
   static Map<String, String> getPhotoNotesFieldMapTest()
   {
      return new Map<String, String>
      {
        'Previous_wall_repair_photo__c' => 'Previous_Wall_Repair_Notes__c',
        'LeaningBowing_Wall_Photo__c'   => 'Leaning_Bowing_Wall_Notes__c',
        'Floor_Crack_Repair_Photo__c'   => 'Floor_Crack_Notes__c',
        'Wall_Crack_Photo__c'           => 'Wall_Crack_Notes__c',
        'Shallow_Footing_Photo__c'      => 'Shallow_Footing_Photo_Notes__c'
      };

      System.assertEquals(5, getPhotoNotesFieldMapTest().values().size());
   }
}

So you can see, I 'think' I have the class structured properly and to me the assert statement even looks good. What is going on?

Best Answer


Hello,

The problem IS the structure.

Let's analyze your code.

You have a test class called : FLLC_Foreman_Job_Packet_Helper_Test In this class you have a method called getPhotoNotesFieldMapTest and I suppose it's a test method so you should have @test also on it. (Point of attention 1)

you have your return statement before the assert ==> the assert will be unreachable (compilation error) (Point of attention 2)

Let's correct all that :)

@isTest
private inherited sharing class FLLC_Foreman_Job_Packet_Helper_Test
{
   static Map<String, String> getPhotoNotesFieldMapTest()
   {
      return new Map<String, String>
      {
        'Previous_wall_repair_photo__c' => 'Previous_Wall_Repair_Notes__c',
        'LeaningBowing_Wall_Photo__c'   => 'Leaning_Bowing_Wall_Notes__c',
        'Floor_Crack_Repair_Photo__c'   => 'Floor_Crack_Notes__c',
        'Wall_Crack_Photo__c'           => 'Wall_Crack_Notes__c',
        'Shallow_Footing_Photo__c'      => 'Shallow_Footing_Photo_Notes__c'
      };

   }

   @istest private static void testMethod1() {
      System.assertEquals(5, getPhotoNotesFieldMapTest().values().size());
   }
}

Now let's talk best practice.

You should separate your code logic and the test class. So to do this in a clean way, you should have two classes:

Classe1: (not a test class)

private inherited sharing class FLLC_Foreman_Job_Packet_Helper
{
   public static Map<String, String> getPhotoNotesFieldMap()
   {
      return new Map<String, String>
      {
        'Previous_wall_repair_photo__c' => 'Previous_Wall_Repair_Notes__c',
        'LeaningBowing_Wall_Photo__c'   => 'Leaning_Bowing_Wall_Notes__c',
        'Floor_Crack_Repair_Photo__c'   => 'Floor_Crack_Notes__c',
        'Wall_Crack_Photo__c'           => 'Wall_Crack_Notes__c',
        'Shallow_Footing_Photo__c'      => 'Shallow_Footing_Photo_Notes__c'
      };

   }

}

class 2: (Test Class)

@isTest
private inherited sharing class FLLC_Foreman_Job_Packet_Helper_TEST
{
   @istest private static void testMethod1() {
      System.assertEquals(5, FLLC_Foreman_Job_Packet_Helper.getPhotoNotesFieldMap().values().size());
   }
}

Hope it could help you :)

Regards

Related Topic