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
rwalrath144rwalrath144 

Customer Web to Case only testing 70%

I am following a template on the Wikki that provides instructions on how to create a customer web-to-case form. I copied and pasted the class code for the custom controller and I am only getting 70% test coverage.

 

public class SubmitCaseController {

 

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;

}

}

}

}

 

 

 http://wiki.developerforce.com/index.php/Creating_Custom_Web-To-Case_Forms_Using_Visualforce_and_Sites

Best Answer chosen by Admin (Salesforce Developers) 
snugglessnuggles

hi rwalrath,

 

i would check the specific line numbers when running the test to see what is not being covered.  i don't see the test method in your comment, but the data in your org could be one reason a test method would pass in another org but not in yours.  You essentially have to make sure all the conditional statements are covered in both directions.  So in this code, you will notice there is an if/else with a few lines in each.  For this you will want two test methods, or two actions within one test method, that will take different paths at this conditional juncture to capture both cases. You can tell what you are missing by examining the test results more closely.

All Answers

snugglessnuggles

hi rwalrath,

 

i would check the specific line numbers when running the test to see what is not being covered.  i don't see the test method in your comment, but the data in your org could be one reason a test method would pass in another org but not in yours.  You essentially have to make sure all the conditional statements are covered in both directions.  So in this code, you will notice there is an if/else with a few lines in each.  For this you will want two test methods, or two actions within one test method, that will take different paths at this conditional juncture to capture both cases. You can tell what you are missing by examining the test results more closely.

This was selected as the best answer
rwalrath144rwalrath144

:smileyhappy:Creating the test method helped. I am not familiar with test methods yet so I had to get some help building one. I am opening another thread for another piece of code I need help creating test methods. But your answer resolved this one.. thanks.