0

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?

  • What is it you are actually trying to accomplish? Currently your unit test just seems to be inspecting its own behavior, but a unit test is supposed to run live code and then verify results. – Adrian Larson Sep 24 '19 at 14:37
1

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

  • OMG you just literally solved my problem. Thank you for the time you took to help me out with this. This is exactly what I needed. Kudos to you man! – TheLizardGod Sep 24 '19 at 15:05
2
  1. Nothing can be written after return statement.
  2. Methods with @IsTest annotation should be void.

What I suggest you to do. Move method getPhotoNotesFieldMap to the separate class, remove Test from method name.

In your test class set ethod:

@IsTest
static void getPhotoNotesFieldMapTest()
{
    Map<String, String> toCheck = YourNewClass.getPhotoNotesFieldMap();

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

  • You are just checking size? Not the values needed?# – Pranay Jaiswal Sep 24 '19 at 14:47
  • I need to technically check both. – TheLizardGod Sep 24 '19 at 14:50
  • I see what you are saying. Okay so I made changes per your instructions and things seem to be validating fine but running the test I am now approached with this error: Method getPhotoNotesFieldMapTest does not exist in the the apex class with Id :01p8A000000nB5p – TheLizardGod Sep 24 '19 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.