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
Charline MitchenerCharline 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;
            }
        }
    }
}
Jigar.LakhaniJigar.Lakhani
Hello,

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:
@isTest
private class SubmitCaseControllerTest {
	static testMethod void testSubmitCase() {
	
		// Create Account
		Account objAccount = new Account;
		objAccount.Name = 'Test Account';
		objAccount.AccountNumber = '12345';
		Insert objAccount;
		
		// Create Contact
		Contact objContact = new Contact;
		objContact.FirstName = 'First Name';
		objContact.LastName = 'Last Name';
		objContact.Email = 'test@test.com';
		Insert objContact;
		
		SubmitCaseController objSubmitCaseController = new SubmitCaseController();
		objSubmitCaseController.UploadFile();
		
		// Cover SOQL queries for account and contact based on below fields
		objSubmitCaseController.acctNum = '12345';
		objSubmitCaseController.c.SuppliedEmail = 'test@test.com';
		
		objSubmitCaseController.submitCase();
	}
}

Thanks and Cheers,
Jigar
PratikPratik (Salesforce Developers) 
Hi Charline,

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
Charline MitchenerCharline Mitchener
Thanks for your assistance with this! Jigar, I ran the test but it only covers 54% of the SubmitCaseController? Not sure what to do! Thanks again for your help