You need to sign in to do that
Don't have an account?
shankar sudalaimani
Test class for Class_controller
Hi Am new to salesforce please help to write the test class for controller
Thanks in Advance
public with sharing class CaseHistoriesComponentController { public Id caseId {get; set;} public cHistories[] histories; public List<cHistories> getHistories() { list<cHistories> histories = new list<cHistories>(); String prevDate = ''; for(CaseHistory cHistory : [Select CreatedDate, CreatedBy.Name, CreatedBy.Id, Field, NewValue, OldValue from CaseHistory where CaseId = :caseId order by CreatedDate desc]) { if((cHistory.newValue == null && cHistory.oldValue == null) || (cHistory.newValue != null && !(string.valueOf(cHistory.newValue).startsWith('005') || string.valueOf(cHistory.newValue).startsWith('00G'))) || (cHistory.oldValue != null && !(string.valueOf(cHistory.oldValue).startsWith('005') || string.valueOf(cHistory.oldValue).startsWith('00G')))) { cHistories tempHistory = new cHistories(); // Set the Date and who performed the action if(String.valueOf(cHistory.CreatedDate) != prevDate) { tempHistory.theDate = String.valueOf(cHistory.CreatedDate); tempHistory.who = cHistory.CreatedBy.Name; tempHistory.userId = cHistory.CreatedBy.Id; } else { tempHistory.theDate = ''; tempHistory.who = ''; tempHistory.userId = cHistory.CreatedBy.Id; } prevDate = String.valueOf(cHistory.CreatedDate); // Get the field label String fieldLabel = TeasTicketGlobalFunction.returnFieldLabel(String.valueOf(cHistory.Field)); // Set the Action value if (String.valueOf(cHistory.Field) == 'created') { // on Creation tempHistory.action = 'Created.'; } else if(cHistory.OldValue != null && cHistory.NewValue == null){ // when deleting a value from a field // Format the Date and if there's an error, catch it and re try { tempHistory.action = 'Deleted ' + Date.valueOf(cHistory.OldValue).format() + ' in <b>' + fieldLabel + '</b>.'; } catch (Exception e){ tempHistory.action = 'Deleted ' + String.valueOf(cHistory.OldValue) + ' in <b>' + fieldLabel + '</b>.'; } } else{ // all other scenarios String fromText = ''; if (cHistory.OldValue != null) { try { fromText = ' from ' + Date.valueOf(cHistory.OldValue).format(); } catch (Exception e) { fromText = ' from ' + String.valueOf(cHistory.OldValue); } } String toText = ''; if (cHistory.OldValue != null) { try { toText = Date.valueOf(cHistory.NewValue).format(); } catch (Exception e) { toText = String.valueOf(cHistory.NewValue); } } if(toText != '') tempHistory.action = 'Changed <b>' + fieldLabel + '</b>' + fromText + ' to <b>' + toText + '</b>.'; else tempHistory.action = 'Changed <b>' + fieldLabel; } // Add to the list histories.add(tempHistory); } } return histories; } // Inner Class to store the detail of the case histories public class cHistories { public String theDate {get; set;} public String who {get; set;} public Id userId {get; set;} public String action {get; set;} } }
Thanks in Advance
Hi,
1. Create an apex class and annotate this class with @isTest as follows
@isTest
private class className {}
2.Create a test method in the above class as follows
static testMethod void verifyHistoryData(){}
3. Create test data for Case and CaseHistory in a method within the test class (You can also use a Util class that creates the data and re-use it here, or use a static resource with csv data and load it here)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
4. Create an instance of your actual class and call the actual method
CaseHistoriesComponentController objClass = new CaseHistoriesComponentController();
objClass.getHistories();
5. This shold help you test the different use cases and achieve desired code coverage
Thanks and Regards
Vivian