function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marco SantosMarco Santos 

Apex code coverage

Hi all
Could somebody tell me how I can write unit test for the following method. I am new to apex so I really have no idea how it should be started, I will be very appreciate for any help
public static void PopulateRate(List<Sales__c> newSalesRecords){
        try {
		
            Map<String, Double> isoCodeToRate=AccountUtils.GetConversionRates();
			
			for (Sales__c sRecord : newSalesRecords)
            {
      
				if (sRecord.Rate__c == null){
					sRecord.Rate__c = isoCodeToRate.get(sRecord.Currency__c.toUpperCase());
				}
            }
        }
        catch (Exception e) {
        	
            throw new SalesTriggerException('Error =' + e.getMessage(), e);
        }
		
}
Thanks in advance
 
Best Answer chosen by Marco Santos
pconpcon
My guess is that's going to return 0% coverage because the class has no methods or anything other than the static variables.   You could try moving it to it's own test class and seeing if that works.
 
public with sharing class CustomException Extends Exception {
    public static final String ERROR_1 = 'Invalid Data 1';
    public static final String ERROR_2 = 'Invalid Data 2';  
}
 
@IsTest
private class CustomExceptionTest {
    public static void testErrorResponses() {
         String expectedError1 = 'Invalid Data 1';
         String expectedError2 = 'Invalid Data 2';

         Test.startTest();

         System.assertEquals(
             expectedError1,
             CustomException.ERROR_1,
             'Did not get the expected error message'
         );
         System.assertEquals(
             expectedError2,
             CustomException.ERROR_2,
             'Did not get the expected error message'
         );

         Test.stopTest();
    }
}

Another way would be to add get methods and test those
 
public with sharing class CustomException Extends Exception {
    public static final String ERROR_1 = 'Invalid Data 1';
    public static final String ERROR_2 = 'Invalid Data 2'; 

    public static String getError1() {
        return ERROR_1;
    }

    public static String getError2() {
        return ERROR_2;
    }
}
 
@IsTest
private class CustomExceptionTest {
    public static void testErrorResponses() {
         String expectedError1 = 'Invalid Data 1';
         String expectedError2 = 'Invalid Data 2';

         Test.startTest();

         System.assertEquals(
             expectedError1,
             CustomException.getError1(),
             'Did not get the expected error message'
         );
         System.assertEquals(
             expectedError2,
             CustomException.getError2(),
             'Did not get the expected error message'
         );

         Test.stopTest();
    }
}

 

All Answers

pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

In your case you'll probably also want to look at how to throw exceptions in tests [4]

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://pcon.github.io/presentations/testing/#exceptional-intro
Marco SantosMarco Santos
Hi
Thanks for the response, as I am trying to understand the unit test and before I move to the code above, could you explain me what should be tested in the the example below, why the code coverage is 0%? Thanks in advance
public with sharing class CustomException Extends Exception{

    public static final String ERROR_1 = 'Invalid Data 1';
    public static final String ERROR_2 = 'Invalid Data 2';
    
    @IsTest
    public static void testErrorResponses()
    {
         System.assertEquals(ERROR_1, ERROR_1);
         System.assertEqualsERROR_2, ERROR_2);
    }

}

 
pconpcon
My guess is that's going to return 0% coverage because the class has no methods or anything other than the static variables.   You could try moving it to it's own test class and seeing if that works.
 
public with sharing class CustomException Extends Exception {
    public static final String ERROR_1 = 'Invalid Data 1';
    public static final String ERROR_2 = 'Invalid Data 2';  
}
 
@IsTest
private class CustomExceptionTest {
    public static void testErrorResponses() {
         String expectedError1 = 'Invalid Data 1';
         String expectedError2 = 'Invalid Data 2';

         Test.startTest();

         System.assertEquals(
             expectedError1,
             CustomException.ERROR_1,
             'Did not get the expected error message'
         );
         System.assertEquals(
             expectedError2,
             CustomException.ERROR_2,
             'Did not get the expected error message'
         );

         Test.stopTest();
    }
}

Another way would be to add get methods and test those
 
public with sharing class CustomException Extends Exception {
    public static final String ERROR_1 = 'Invalid Data 1';
    public static final String ERROR_2 = 'Invalid Data 2'; 

    public static String getError1() {
        return ERROR_1;
    }

    public static String getError2() {
        return ERROR_2;
    }
}
 
@IsTest
private class CustomExceptionTest {
    public static void testErrorResponses() {
         String expectedError1 = 'Invalid Data 1';
         String expectedError2 = 'Invalid Data 2';

         Test.startTest();

         System.assertEquals(
             expectedError1,
             CustomException.getError1(),
             'Did not get the expected error message'
         );
         System.assertEquals(
             expectedError2,
             CustomException.getError2(),
             'Did not get the expected error message'
         );

         Test.stopTest();
    }
}

 
This was selected as the best answer
Marco SantosMarco Santos
Hi pcon, thank you for the responses that where very helpful, thanks again