You need to sign in to do that
Don't have an account?
Rakesh Imsani
How to write test class for this class
Public class CustomCaseAlertsController
{
public Case caseObj {get; set;}
public boolean hasAlerts
{
get{ return (Alerts.size()==0? false: true); }
set;
}
public integer popupWindowHeight
{
get{ return (230+Alerts.size()*40); }
set;
}
private Set<ID> hattrixParentIds=new Set<ID>();
private Set<ID> queryAlertIds=new set<ID>();
public List<soalert__Hidden_Alert__c> Alerts {get; set;}
private List<Hattrix_Account__c> listOfHattrixParent=new List<Hattrix_Account__c>();
public List<soalert__Hidden_Alert__c> userSelectedAlerts=new List<soalert__Hidden_Alert__c>();
public CustomCaseAlertsController(ApexPages.StandardController stdController)
{
String caseId = stdController.getId();
Alerts = new List<soalert__Hidden_Alert__c>();
Map<Id, soalert__Hidden_Alert__c> mapAlertIdToWrapper = new Map<Id, soalert__Hidden_Alert__c>();
caseObj = [Select id,AccountId,ContactId,Site__c,Site__r.Name,CaseNumber from Case where id=:caseId];
for(Hattrix_Account__c idObj:[select id,Hatrix_Parent_Account__c from Hattrix_Account__c where Hatrix_Parent_Account__c =:caseObj.site__c or Hatrix_Parent_Account__c=:caseObj.accountID])
{
hattrixParentIds.add(idObj.id);
}
for(soalert__Alert__c alertObj: [SELECT Id,Name,soalert__Account__c,soalert__Account__r.Name,soalert__Contact__c,soalert__Contact__r.Name,soalert__Alert_Begin_Date__c,soalert__Alert_End_Date__c,soalert__Alert_Message__c,soalert__Alert_Status__c,soalert__Case__c,soalert__User__c FROM soalert__Alert__c
Where ((soalert__Case__c=:caseObj.Id or soalert__Account__c=:caseObj.AccountId or soalert__Account__c=:caseobj.Site__c or soalert__Account__c in:hattrixParentIds or soalert__Contact__c=:caseObj.ContactId) and (soalert__Alert_Begin_Date__c <= today and soalert__Alert_End_Date__c >= today))] )
{
soalert__Hidden_Alert__c alertWrapper = new soalert__Hidden_Alert__c();
alertWrapper.soalert__Alert__c=alertObj.Id;
alertWrapper.soalert__Hidden__c = false;
alertWrapper.soalert__Alert_Message__c = alertObj.soalert__Alert_Message__c;
alertWrapper.soalert__User__c = UserInfo.getUserId();
if(alertObj.soalert__Account__c != null)
{
alertWrapper.soalert__Object_ID__c=alertObj.soalert__Account__c;
alertWrapper.soalert__Object_Type__c=alertObj.soalert__Account__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Record_Name__c = alertObj.soalert__Account__r.Name;
System.debug('alertObj.soalert__Account__c.getSObjectType().getDescribe().getLabel();'+alertObj.soalert__Account__c.getSObjectType().getDescribe().getLabel());
}
else if(alertObj.soalert__Contact__c !=null)
{
// alertWrapper.soalert__Object_Type__c=alertObj.soalert__Contact__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Object_ID__c=alertObj.soalert__Contact__c;
alertWrapper.soalert__Object_Type__c=alertObj.soalert__Contact__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Record_Name__c = alertObj.soalert__Contact__r.Name;
}
else if(alertObj.soalert__Case__c !=null)
{
System.debug('case');
//alertWrapper.soalert__Object_Type__c=alertObj.soalert__Case__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Object_ID__c=alertObj.soalert__Case__c;
alertWrapper.soalert__Object_Type__c=alertObj.soalert__Case__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Record_Name__c = caseObj.CaseNumber;// need to clarify
}
mapAlertIdToWrapper.put(alertObj.Id, alertWrapper);
//Alerts.add(alertWrapper);
}
for (soalert__Hidden_Alert__c hiddenObj: [Select id,soalert__Alert__c,soalert__User__c from soalert__Hidden_Alert__c
where (soalert__User__c=:UserInfo.getUserID() and soalert__Hidden__c=true
and soalert__Alert__c in : mapAlertIdToWrapper.KeySet())])
{
mapAlertIdToWrapper.remove(hiddenObj.soalert__Alert__c);
}
Alerts = mapAlertIdToWrapper.values();
}
public void hideAlerts()
{
//Method for hiding the selected alerts
for(soalert__Hidden_Alert__c c: Alerts)
{
if(c.soalert__Hidden__c==true )
{
userSelectedAlerts.add(c);
}
}
insert userSelectedAlerts;
}
}
{
public Case caseObj {get; set;}
public boolean hasAlerts
{
get{ return (Alerts.size()==0? false: true); }
set;
}
public integer popupWindowHeight
{
get{ return (230+Alerts.size()*40); }
set;
}
private Set<ID> hattrixParentIds=new Set<ID>();
private Set<ID> queryAlertIds=new set<ID>();
public List<soalert__Hidden_Alert__c> Alerts {get; set;}
private List<Hattrix_Account__c> listOfHattrixParent=new List<Hattrix_Account__c>();
public List<soalert__Hidden_Alert__c> userSelectedAlerts=new List<soalert__Hidden_Alert__c>();
public CustomCaseAlertsController(ApexPages.StandardController stdController)
{
String caseId = stdController.getId();
Alerts = new List<soalert__Hidden_Alert__c>();
Map<Id, soalert__Hidden_Alert__c> mapAlertIdToWrapper = new Map<Id, soalert__Hidden_Alert__c>();
caseObj = [Select id,AccountId,ContactId,Site__c,Site__r.Name,CaseNumber from Case where id=:caseId];
for(Hattrix_Account__c idObj:[select id,Hatrix_Parent_Account__c from Hattrix_Account__c where Hatrix_Parent_Account__c =:caseObj.site__c or Hatrix_Parent_Account__c=:caseObj.accountID])
{
hattrixParentIds.add(idObj.id);
}
for(soalert__Alert__c alertObj: [SELECT Id,Name,soalert__Account__c,soalert__Account__r.Name,soalert__Contact__c,soalert__Contact__r.Name,soalert__Alert_Begin_Date__c,soalert__Alert_End_Date__c,soalert__Alert_Message__c,soalert__Alert_Status__c,soalert__Case__c,soalert__User__c FROM soalert__Alert__c
Where ((soalert__Case__c=:caseObj.Id or soalert__Account__c=:caseObj.AccountId or soalert__Account__c=:caseobj.Site__c or soalert__Account__c in:hattrixParentIds or soalert__Contact__c=:caseObj.ContactId) and (soalert__Alert_Begin_Date__c <= today and soalert__Alert_End_Date__c >= today))] )
{
soalert__Hidden_Alert__c alertWrapper = new soalert__Hidden_Alert__c();
alertWrapper.soalert__Alert__c=alertObj.Id;
alertWrapper.soalert__Hidden__c = false;
alertWrapper.soalert__Alert_Message__c = alertObj.soalert__Alert_Message__c;
alertWrapper.soalert__User__c = UserInfo.getUserId();
if(alertObj.soalert__Account__c != null)
{
alertWrapper.soalert__Object_ID__c=alertObj.soalert__Account__c;
alertWrapper.soalert__Object_Type__c=alertObj.soalert__Account__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Record_Name__c = alertObj.soalert__Account__r.Name;
System.debug('alertObj.soalert__Account__c.getSObjectType().getDescribe().getLabel();'+alertObj.soalert__Account__c.getSObjectType().getDescribe().getLabel());
}
else if(alertObj.soalert__Contact__c !=null)
{
// alertWrapper.soalert__Object_Type__c=alertObj.soalert__Contact__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Object_ID__c=alertObj.soalert__Contact__c;
alertWrapper.soalert__Object_Type__c=alertObj.soalert__Contact__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Record_Name__c = alertObj.soalert__Contact__r.Name;
}
else if(alertObj.soalert__Case__c !=null)
{
System.debug('case');
//alertWrapper.soalert__Object_Type__c=alertObj.soalert__Case__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Object_ID__c=alertObj.soalert__Case__c;
alertWrapper.soalert__Object_Type__c=alertObj.soalert__Case__c.getSObjectType().getDescribe().getLabel();
alertWrapper.soalert__Record_Name__c = caseObj.CaseNumber;// need to clarify
}
mapAlertIdToWrapper.put(alertObj.Id, alertWrapper);
//Alerts.add(alertWrapper);
}
for (soalert__Hidden_Alert__c hiddenObj: [Select id,soalert__Alert__c,soalert__User__c from soalert__Hidden_Alert__c
where (soalert__User__c=:UserInfo.getUserID() and soalert__Hidden__c=true
and soalert__Alert__c in : mapAlertIdToWrapper.KeySet())])
{
mapAlertIdToWrapper.remove(hiddenObj.soalert__Alert__c);
}
Alerts = mapAlertIdToWrapper.values();
}
public void hideAlerts()
{
//Method for hiding the selected alerts
for(soalert__Hidden_Alert__c c: Alerts)
{
if(c.soalert__Hidden__c==true )
{
userSelectedAlerts.add(c);
}
}
insert userSelectedAlerts;
}
}
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.NOTE: Please use the "Add a code sample" button (icon <>) when pasting code. This will increase readability and give the responder line numbers they can use.
[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/
I have to agree with pcon's point - it's hard to tell how to create a test class for your entire Apex controller. I typically approach the test class by identifying the key test scenarios and logical conditions and list each one of them as a test method (kinda similar of how you develop test scripts), even follow the similar steps pcon mentioned about for each test method.