function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Katie ChristensenKatie 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;

        }       

    }  

}

 
Best Answer chosen by Katie Christensen
JayantJayant
Sorry, blunder from my end. stopTest exists but startTest does not.

@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

Katie ChristensenKatie Christensen
Hi Jayant,
I should have been more clear... I need help writing the test class. My overall org code coverage is 73%.
Katie ChristensenKatie Christensen
VF Page = New_Case3
Katie ChristensenKatie Christensen
It allowed me to deploy a version on 4/9. I made some modifications and tried to deploy the final version tonight and now it's giving me a code coverage error. I assumed I need to create a test class for it so not really sure what to do at this point...
JayantJayant
Just to re-iterate, please use this just as a hotfix for increasing code coverage for your urgent deployment.
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).
Katie ChristensenKatie Christensen
I was able to save the test case and it increased the code coverage to 74%. I received the following failure details:

Code Coverage Failure
Your organization's code coverage is 74%. You need at least 75% coverage to complete this deployment.

User-added image
Katie ChristensenKatie Christensen
Line 51 in SubmitCaseController:   c.setOptions(dmlOpts);
Katie ChristensenKatie Christensen
Still getting the following code coverage error

User-added image
JayantJayant
Are you using the code from last post ? It seems that its not saved then.
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();
        }            
    }
}
Katie ChristensenKatie Christensen
Sorry - you're correct. I failed to save the changes. Unfortunately getting a new error: 
User-added image
JayantJayant
Sorry, blunder from my end. stopTest exists but startTest does not.

@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();
        }            
    }
}
This was selected as the best answer
Katie ChristensenKatie Christensen
Success! Test passed and I was able to deploy the changes to production. Thank you for your guidance and persistance Jayant! 
JayantJayant
You are most welcome.
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.