Category: tdd

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

JSON Parsing with Apex in Salesforce – Part I

Here, I would like to share an example of very basic JSON parsing with Apex programming language in Salesforce.MainWrapper Class:public class MainWrapper { public Integer studentId; public String studentName; public String address; public MainWrapper(Integer studentId, String studentName, String address) { this.studentId = studentId; this.studentName = studentName; this.address = address; }}And now the test class to check whether JSON Parsing is working fine or not.@isTestprivate class TestMainWrapper { static testMethod void testJsonParser() { String jsonMessage = ‘{“studentId” : 1, “studentName” : “Sudipta Deb”, “address” : “Kolkata, India”}’; JSONParser parser = JSON.createParser(jsonMessage); MainWrapper m = (MainWrapper)parser.readValueAs(MainWrapper.class); System.assertEquals(1, m.studentId); System.assertEquals(‘Sudipta Deb’, m.studentName); System.assertEquals(‘Kolkata, India’, m.address); }}I will share more complex JSON Parsing very soon. Any feedback is always...

Read More

Custom Logging Framework in Salesforce

In my current project, I have a requirement where I have to implement a custom logging framework. Salesforce put the debug/exception log with System.debug() statement, but going through that debug log to identify the issue is always a challenge. So the better approach is to store the exception as soon as it occurred somewhere so that later point we can always refer that to identify what happened.In order to achieve that below is what I did-First I have created an object(API Name: Custom_Log__c) to store the exception logs-Below are the custom field inside the object-Class__c(Text Area(255))   From which class the exception is comingMethod__c(Text Area(255))  From which method the exception is comingMessage__c(Long Text Area(32768))  Stack trace of the exceptionPriority__c(Picklist) – Possible values are DEBUG, ERROR, FATAL, INFO, WARNINGNow I need to create the below custom settings -EnableCustomLogging with possible values true/false. The reason behind is that with EnableCustomLogging = true, exceptions will be stored in the custom object i.e. Custom_Log__c and with EnableCustomLogging = false, exception will be stored in the debug log with System.debug() Now with this setup, I have created the visual page.Source Code:<apex:page controller=”CustomLogController”> <apex:form > <apex:commandButton value=”Divide By Zero Exception!” action=”{!divideByZeroException}” /> <br/> <apex:commandButton value=”Null Pointer Exception!” action=”{!nullPointerException}” /> </apex:form></apex:page>Now the controller – CustomLogController:public class CustomLogController { public void divideByZeroException(){ Integer number1, number2; try{ number1 = 10; number2 = 0; Integer number3 = number1/number2; }catch(Exception ex){ String message = ‘Error: ‘ +...

Read More
Loading

Earlier Posts

  • 2024 (31)
  • 2023 (47)
  • 2022 (25)
  • 2021 (35)
  • 2020 (41)
  • 2019 (13)
  • 2018 (16)
  • 2017 (14)
  • 2016 (18)
  • 2015 (74)
  • 2014 (20)