Category: unit testing

Salesforce Summer 15 New Feature || New way to calculate code coverage for multiline statements

In Summer 15 release, Salesforce changed the way to calculate code coverage for multiline statements. As a developer, you feel good in some situation and bad in some situations. In this post, I will explain both the situations.Let me first explain the change -If a single statement is broken into multiple lines, then each line will be considered now while calculating the code coverage. To be more precise, each line that contains an expression will be considered while calculating code coverage. Before Summer 15 release, multiline was considered as a single line.To explain more, I will start with a simple code snippet.Situation 1 – Happy Situation:Consider the below code snippet.Now say for example, you have written unit test methods for the methods calculateDistanceBetweenAB(), calculateDistanceBetweenBC(), calculateDistanceBetweenCD() and calculateDistanceBetweenDA().You have also written test methods to cover line# 8,9 and 10. The only line which is not covered/tested is line# 11.Now before Summer 15 release, the way in which code overage was calculated -Total number of lines = 4 (line # 3,8,9 and 11)Total number of lines covered = 3 (line # 3,8 and 9)So the code coverage = 3/4 i.e. 75%But after Summer 15 release, the way in which code overage will be calculated -Total number of lines = 7 (line # 3,4,5,6,8,9 and 11)Total number of lines covered = 6 (line # 3,4,5,6,8 and 9)So the code coverage = 6/7...

Read More

How to write unit test classes for @future methods

In this post, I will explain how to write unit test classes to increase test coverage for @future methods. Let’s start with a Use Case -Use Case -Let’s say in my Account object, I have a checkbox named “notifyAdmin__c” and I would like to update this checkbox for list of Accounts in a future method.Below is the Apex Class -Now let’s write the test class. Below is the test class -Here is the explanation-To test future methods, you need to simply make your future method call between Test.startTest() and Test.stopTest(). Test.stopTest() will make sure that @future method will have fired. So after Test.stopTest(), you can do all your assert.I hope this quick example will help you to write unit test cases for your @future method. If you have any suggestion, please let me...

Read More

Spring ’15 – New Feature Added – @testSetup

One of the very interesting feature added in Spring ’15 is @testSetup. In this post I will try to explain with example more about the feature. Below is what you will get from release note about @testSetup.Now let’s check with example -Below is the class/methods for which we need to write test methods – public class AccountHelper { public List<Account> getAllAccounts(){ List<Account> allAccounts = [Select Id from Account]; return allAccounts; } public Integer getEmployeeCountFrom(String accountNumber){ Account fetchedAccount = [Select Id, NumberOfEmployees from Account where AccountNumber = :accountNumber LIMIT 1]; return fetchedAccount.NumberOfEmployees; } public List<Case> getAllCases(Id accountId){ return [select ID from Case where AccountId = :accountId]; } public void updateEmployeeCountFor(String accountNumber, Integer newEmployeeCount){ Account fetchedAccount = [Select Id, NumberOfEmployees from Account where AccountNumber = :accountNumber LIMIT 1]; fetchedAccount.NumberOfEmployees = newEmployeeCount; update fetchedAccount; }}Previously we used to write test classes like below -@isTestpublic class AccountHelperTest { static testMethod void testGetAllAccounts(){ List<Account> accounts = new List<Account>(); for(Integer i=0;i < 100;i++){ accounts.add(new Account(Name = ‘Universal Container’)); } insert accounts; Test.startTest(); AccountHelper accountHelper = new AccountHelper(); List<Account> allAccounts = accountHelper.getAllAccounts(); Test.stopTest(); System.assertEquals(100, allAccounts.size()); } static testMethod void testGetEmployeeCountFrom(){ Account anAccount = new Account(); anAccount.Name = ‘Universal Container’; anAccount.AccountNumber = ‘TEST’; anAccount.NumberOfEmployees = 100; insert anAccount; Test.startTest(); AccountHelper accountHelper = new AccountHelper(); Integer numberOfEmployees = accountHelper.getEmployeeCountFrom(‘TEST’); Test.stopTest(); System.assertEquals(100, numberOfEmployees); } static testMethod void testGetAllCases(){ //Create the account first Account anAccount = new Account(); anAccount.Name = ‘Universal Container’; insert anAccount;...

Read More

JSON Parsing with Apex in Salesforce – Part II

This post is in continuation of my previous post regarding JSON Parsing.In this post, what I am going to share is-How to parse a little more complex JSON containing array. Once the JSON data is parsed, how to display the same information in Visual Force Page.Check the below link to understand what I am trying to achieve at the end of this posthttp://sudipta-developer-edition.ap1.force.com/studentdetailsSo let’s start with creating the Visual Force Page<apex:page controller=”StudentDetailController”> <apex:form > <apex:pageBlock Title=”JSON Parser Example”> <apex:commandButton value=”Parse JSON Data” action=”{!parseJSONData}” reRender=”studentData” /> </apex:pageBlock> <br /> <apex:pageBlock id=”studentData”> First Name: <apex:inputText value=”{!firstName}” /> Last Name: <apex:inputText value=”{!lastName}” /> <br/> <apex:pageBlockSection title=”Language Known:” collapsible=”false”> <apex:selectList value=”{!selectedLanguage}” multiselect=”false” size=”1″> <apex:selectOptions value=”{!languageOptions}”/> </apex:selectList> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>Now the controller-public class StudentDetailController{ public String firstName{get;set;} public String lastName{get;set;} public String selectedLanguage{get;set;} private StudentDetailWrapper m; public List<SelectOption> languageOptions; public void parseJSONData(){ m = new ParseStudentData().parseData(); doAllInitialization(); } public void parseJSONData(String jsonMessage){ m = new ParseStudentData().parseData(jsonMessage); doAllInitialization(); } public List<SelectOption> getLanguageOptions(){ if(null == languageOptions){ languageOptions = new List<SelectOption>(); } return languageOptions; } private void doAllInitialization(){ initializeStudentName(); initializeLanguageOptions(m.language); } private void initializeStudentName(){ this.firstName = m.firstName; this.lastName = m.lastName; } private void initializeLanguageOptions(List<String> language){ languageOptions = new List<SelectOption>(); for(String aLanguage : language){ languageOptions.add(new SelectOption(aLanguage, aLanguage)); } }}Now the class which will do the JSON Parsing-public class ParseStudentData { private StudentDetailWrapper m; public StudentDetailWrapper parseData(String jsonMessage){ return doParsing(jsonMessage); } public StudentDetailWrapper parseData(){ String jsonMessage = ‘{“firstName” : “Sudipta”,...

Read More

Learn & Practice Salesforce directly from Salesforce – TrailHead – What an initiative

In Dreamforce 2014, Salesforce announces a platform where developers can learn Salesforce by their own. WoW!!! It is TrailHead.Though it is in a beta stage, but already this platform is having quite a good amount of tutorials. But what I like most is that TrailHead is having a very cool feature – Challenge yourself. Here what you can do is that you can challenge yourself to complete the exercises and earn points. The intention behind is to check your understanding.I am already in love with this initiative. Guys, please go ahead and check this one.Salesforce...

Read More
Loading