You need to sign in to do that
Don't have an account?
Marco 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
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
Another way would be to add get methods and test those
All Answers
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
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
Another way would be to add get methods and test those