+ Start a Discussion

How do I create/write a test method

New to Apex development and looking for some help.  I have added some custom VisualForce pages and Apex code, all of which are working just fine.  I need to deploy this to production but an not sure how to write the necessary test methods


The code is rather simply so I have to assume that the test methods will be simple as well.  Any help, code samples would be greatly appreciated.


Apex Class


public class CustomController{ Project_KA__c myProject; Project_KA__c myProjectDetails; List<Case> relatedCases; public Case selectedCase { get; set; } private final ApexPages.StandardController controller; public CustomController(ApexPages.StandardController controller){ this.controller = controller; myProject = (Project_KA__c)controller.getRecord(); } //Method to return Project details to VF Page public Project_KA__c getProjectDetails(){ myProjectDetails = [select Id, Name, Project_Type__c from Project_KA__c where Id = :myProject.Id]; return myProjectDetails; } //Method to return relatedcases to VF page (MultiCase) public List<Case> getrelatedCases(){ relatedCases = [select Id, CaseNumber, Subject, Status, Date_Du__c, Internal_Date_Due__c, OwnerID from Case where Project__c = :myProject.Id ORDER BY Date_Du__c]; return relatedCases; } }

 Thanks in advance



Can you please post your test method created once completed here. so others can learn. I'm having the same issue. I hate writing code at the best of times.

and Thanks Here is the completed test class I created that fully validated and has been promted to prduction.



@isTest private class CustomControllerTest { static testMethod void validateRelatedCases() { //Insert new project Project_KA__c p = new Project_KA__c(name='T1 Project'); insert p; //Insert new case Case c = new Case(status='Open', subject='Test Case', Date_Du__c=System.today(), Internal_Date_Due__c=System.today()); insert c; //Select the new case back out c = [SELECT Subject FROM Case WHERE Id=:c.id]; //Test that the add/edit case worked System.assertEquals('Test Case',c.subject); //Test that the retreive new project worked p = [SELECT Id,Name FROM Project_KA__c WHERE Id=:p.id]; System.assertEquals('T1 Project',p.name); } }