You need to sign in to do that
Don't have an account?
Katie Christensen
Help with Controller Extension test method
Hi All,
I have been given this code however I am not a developer and received an error that my test coverage was only 73%.
I have searched a ton of test code but I'm still lost and need to get this deployed as soon as possible.
Any help would be much appreciated.
Thank you in advance!
Class
public class SubmitCaseController {
private final Case c;
public SubmitCaseController(ApexPages.StandardController stdcontroller) {
this.c = (Case)stdcontroller.getRecord();
}
public PageReference redirectByProfile()
{
Id profileId = userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
string mystring1 = 'Portal';
if (profileName.contains(myString1)==false)
{
String prefix = Case.SObjectType.getDescribe().getKeyPrefix();
String param = getParameters() + '&(your field id)=(your field value)';
return new PageReference('/'+prefix+'/e?nooverride=1&'+param);
}
else
return null;
}
private String getParameters(){
string param = '';
Map <String, String> strMap = ApexPages.currentPage().getParameters();
String[] keys = new String[]{'RecordType', 'retURL', 'cancelURL'};
for(String s : keys){
if(strMap.containsKey(S)) param += s + '=' + strMap.get(s) + '&';
}
if(param.length() > 0) param = param.substring(0, param.length()-1);
return param;
}
public SubmitCaseController() {
AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
c.setOptions(dmlOpts);
}
public static list<SelectOption> getFieldValues(SObject obj, String fld) {
List<SelectOption> options = new List<SelectOption>();
map<String, Schema.SObjectField>fieldMap = obj.getSObjectType().getDescribe().fields.getMap();
list<Schema.PicklistEntry> values =
fieldMap.get(fld).getDescribe().getPickListValues();
options.add(new SelectOption('', '--None--'));
for (Schema.PicklistEntry a : values) {
options.add(new SelectOption(a.getLabel(), a.getValue()));
}
return options;
}
public PageReference submitCase() {
try {
c.Origin = 'Communities';
INSERT c;
PageReference casePage = new ApexPages.StandardController(c).view();
casePage.setRedirect(true);
return casePage;
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
}
I have been given this code however I am not a developer and received an error that my test coverage was only 73%.
I have searched a ton of test code but I'm still lost and need to get this deployed as soon as possible.
Any help would be much appreciated.
Thank you in advance!
Class
public class SubmitCaseController {
private final Case c;
public SubmitCaseController(ApexPages.StandardController stdcontroller) {
this.c = (Case)stdcontroller.getRecord();
}
public PageReference redirectByProfile()
{
Id profileId = userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
string mystring1 = 'Portal';
if (profileName.contains(myString1)==false)
{
String prefix = Case.SObjectType.getDescribe().getKeyPrefix();
String param = getParameters() + '&(your field id)=(your field value)';
return new PageReference('/'+prefix+'/e?nooverride=1&'+param);
}
else
return null;
}
private String getParameters(){
string param = '';
Map <String, String> strMap = ApexPages.currentPage().getParameters();
String[] keys = new String[]{'RecordType', 'retURL', 'cancelURL'};
for(String s : keys){
if(strMap.containsKey(S)) param += s + '=' + strMap.get(s) + '&';
}
if(param.length() > 0) param = param.substring(0, param.length()-1);
return param;
}
public SubmitCaseController() {
AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
c.setOptions(dmlOpts);
}
public static list<SelectOption> getFieldValues(SObject obj, String fld) {
List<SelectOption> options = new List<SelectOption>();
map<String, Schema.SObjectField>fieldMap = obj.getSObjectType().getDescribe().fields.getMap();
list<Schema.PicklistEntry> values =
fieldMap.get(fld).getDescribe().getPickListValues();
options.add(new SelectOption('', '--None--'));
for (Schema.PicklistEntry a : values) {
options.add(new SelectOption(a.getLabel(), a.getValue()));
}
return options;
}
public PageReference submitCase() {
try {
c.Origin = 'Communities';
INSERT c;
PageReference casePage = new ApexPages.StandardController(c).view();
casePage.setRedirect(true);
return casePage;
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
}
@isTest(SeeAllData=true)
public class Test_SubmitCaseController {
private static testmethod void testSubmitCase1()
{
Test.startTest();
Case case1 = [SELECT Id, RecordTypeId, CaseNumber, Type, Origin FROM Case WHERE Status = 'Open' LIMIT 1];
List<Case> cases = new List<Case>{case1};
Case case2 = cases.deepClone(false,false,false)[0];
case2.Id = null;
ApexPages.StandardController stdCon = new ApexPages.StandardController(case2);
Test.setCurrentPage(Page.New_Case3);
ApexPages.currentPage().getParameters().put('RecordType', case1.RecordTypeId);
SubmitCaseController sccExt = new SubmitCaseController(stdCon);
SubmitCaseController.getFieldValues(case1, 'Origin');
sccExt.redirectByProfile();
sccExt.submitCase();
Test.stopTest();
}
private static testmethod void testSubmitCase2()
{
User portalUser = [SELECT Id FROM User WHERE isActive = TRUE AND IsPortalEnabled = TRUE LIMIT 1];
System.runAs(portalUser){
Test.startTest();
Case case1 = [SELECT Id, RecordTypeId, CaseNumber, Type, Origin FROM Case WHERE Status = 'Open' LIMIT 1];
List<Case> cases = new List<Case>{case1};
Case case2 = cases.deepClone(false,false,false)[0];
case2.Id = null;
ApexPages.StandardController stdCon = new ApexPages.StandardController(case2);
Test.setCurrentPage(Page.New_Case3);
ApexPages.currentPage().getParameters().put('RecordType', case1.RecordTypeId);
SubmitCaseController sccExt = new SubmitCaseController(stdCon);
sccExt.submitCase();
Test.stopTest();
}
}
}
All Answers
I should have been more clear... I need help writing the test class. My overall org code coverage is 73%.
Please contact your developer to get a better test class written for this as per your organization's coding standards and following the best practices (which are not followed here for sure).
Code Coverage Failure
Your organization's code coverage is 74%. You need at least 75% coverage to complete this deployment.
You don't have any developers to support during deployments ? Some developer who has access to your org can help you.
Find below -
@isTest(SeeAllData=true)
public class Test_SubmitCaseController {
private static testmethod void testSubmitCase1()
{
Case case1 = [SELECT Id, RecordTypeId, CaseNumber, Type, Origin FROM Case WHERE Status = 'Open' LIMIT 1];
List<Case> cases = new List<Case>{case1};
Case case2 = cases.deepClone(false,false,false)[0];
case2.Id = null;
ApexPages.StandardController stdCon = new ApexPages.StandardController(case2);
Test.setCurrentPage(Page.New_Case3);
ApexPages.currentPage().getParameters().put('RecordType', case1.RecordTypeId);
SubmitCaseController sccExt = new SubmitCaseController(stdCon);
SubmitCaseController.getFieldValues(case1, 'Origin');
sccExt.redirectByProfile();
sccExt.submitCase();
Test.stopTest();
}
private static testmethod void testSubmitCase2()
{
User portalUser = [SELECT Id FROM User WHERE isActive = TRUE AND IsPortalEnabled = TRUE LIMIT 1];
System.runAs(portalUser){
Case case1 = [SELECT Id, RecordTypeId, CaseNumber, Type, Origin FROM Case WHERE Status = 'Open' LIMIT 1];
List<Case> cases = new List<Case>{case1};
Case case2 = cases.deepClone(false,false,false)[0];
case2.Id = null;
ApexPages.StandardController stdCon = new ApexPages.StandardController(case2);
Test.setCurrentPage(Page.New_Case3);
ApexPages.currentPage().getParameters().put('RecordType', case1.RecordTypeId);
SubmitCaseController sccExt = new SubmitCaseController(stdCon);
sccExt.submitCase();
Test.stopTest();
}
}
}
@isTest(SeeAllData=true)
public class Test_SubmitCaseController {
private static testmethod void testSubmitCase1()
{
Test.startTest();
Case case1 = [SELECT Id, RecordTypeId, CaseNumber, Type, Origin FROM Case WHERE Status = 'Open' LIMIT 1];
List<Case> cases = new List<Case>{case1};
Case case2 = cases.deepClone(false,false,false)[0];
case2.Id = null;
ApexPages.StandardController stdCon = new ApexPages.StandardController(case2);
Test.setCurrentPage(Page.New_Case3);
ApexPages.currentPage().getParameters().put('RecordType', case1.RecordTypeId);
SubmitCaseController sccExt = new SubmitCaseController(stdCon);
SubmitCaseController.getFieldValues(case1, 'Origin');
sccExt.redirectByProfile();
sccExt.submitCase();
Test.stopTest();
}
private static testmethod void testSubmitCase2()
{
User portalUser = [SELECT Id FROM User WHERE isActive = TRUE AND IsPortalEnabled = TRUE LIMIT 1];
System.runAs(portalUser){
Test.startTest();
Case case1 = [SELECT Id, RecordTypeId, CaseNumber, Type, Origin FROM Case WHERE Status = 'Open' LIMIT 1];
List<Case> cases = new List<Case>{case1};
Case case2 = cases.deepClone(false,false,false)[0];
case2.Id = null;
ApexPages.StandardController stdCon = new ApexPages.StandardController(case2);
Test.setCurrentPage(Page.New_Case3);
ApexPages.currentPage().getParameters().put('RecordType', case1.RecordTypeId);
SubmitCaseController sccExt = new SubmitCaseController(stdCon);
sccExt.submitCase();
Test.stopTest();
}
}
}
Please do get it reviewed and updated by a developer from your organization as per the coding guidelines that you follow. It's just a hotfix.