You need to sign in to do that
Don't have an account?
Charline Mitchener
Help with test class
I have created a VF Page that allowed users to submit a case. When I saved it, Salesforce prompted me to create controllers for this page.
I have very limited experience when it comes to Apex Classes and although I understand the basics of what a test class is, I have no idea how to write one.. can someone please help!
The VF Page will be used in our community to allows Community users to submit a case to support.
Below is the controller that Salesforce created for me -
public class SubmitCaseController {
public String parentId { get; set; }
public String Attach { get; set; }
public String contentType { get; set; }
public String showAttach { get; set; }
public PageReference UploadFile() {
return null;
}
public String fileBody { get; set; }
public String fileName { get; set; }
public Case c { get; set; }
public String acctNum { get; set; }
public SubmitCaseController() {
c = new Case();
}
public PageReference submitCase() {
List<Account> accts = [SELECT Id FROM Account WHERE AccountNumber = :acctNum];
if (accts.size() != 1) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
ApexPages.addMessage(msg);
return null;
} else {
try {
c.AccountId = accts.get(0).Id;
// now look for an associated contact with the same email
Contact cnt = [SELECT Id FROM Contact WHERE AccountId = :c.AccountId AND Email = :c.SuppliedEmail LIMIT 1];
if (cnt != null)
c.ContactId = cnt.Id;
// Specify DML options to ensure the assignment rules are executed
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.useDefaultRule = true;
c.setOptions(dmlOpts);
// Insert the case
INSERT c;
return new PageReference('/thanks');
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
}
}
I have very limited experience when it comes to Apex Classes and although I understand the basics of what a test class is, I have no idea how to write one.. can someone please help!
The VF Page will be used in our community to allows Community users to submit a case to support.
Below is the controller that Salesforce created for me -
public class SubmitCaseController {
public String parentId { get; set; }
public String Attach { get; set; }
public String contentType { get; set; }
public String showAttach { get; set; }
public PageReference UploadFile() {
return null;
}
public String fileBody { get; set; }
public String fileName { get; set; }
public Case c { get; set; }
public String acctNum { get; set; }
public SubmitCaseController() {
c = new Case();
}
public PageReference submitCase() {
List<Account> accts = [SELECT Id FROM Account WHERE AccountNumber = :acctNum];
if (accts.size() != 1) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
ApexPages.addMessage(msg);
return null;
} else {
try {
c.AccountId = accts.get(0).Id;
// now look for an associated contact with the same email
Contact cnt = [SELECT Id FROM Contact WHERE AccountId = :c.AccountId AND Email = :c.SuppliedEmail LIMIT 1];
if (cnt != null)
c.ContactId = cnt.Id;
// Specify DML options to ensure the assignment rules are executed
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.useDefaultRule = true;
c.setOptions(dmlOpts);
// Insert the case
INSERT c;
return new PageReference('/thanks');
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
}
}
For test class first of all you need to understand the apex class with which objects are using in it and which SOQL queries are executed.
Then after you can create records(account and contact here) in test class which will be retreieved in your SOQL queries of apex class.
And for coverage of methods you just need to create apex class object and you can write Object.MethodName(); which will execute class method based on created records in test class.
Test class for your apex class:
Thanks and Cheers,
Jigar
You can get started here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
Thanks,
Pratik