You need to sign in to do that
Don't have an account?
michael32
Problem writing test class for a Visualforce controller extension
I have an email publisher that populates based on the value in the case type field. However, I have only been able to get 40% coverage. Any idea what I am doing wrong?
Visualforce page with email publisher:
<apex:page standardController="Case" extensions="myControllerExtention2"> <apex:emailPublisher entityId="{!case.id}" emailBody="{!EmailBody}" subject="{!Subject}" /> </apex:page>
Extension that contains logic and content for the email publisher:
public class myControllerExtention2 { private Case cas; private string EmailBody, Subject; //Define default values String defaultEmailBody = ''; String defaultSubject = ''; //Instantiate Visualforce standard controller public myControllerExtention2(ApexPages.StandardController stdController) { //Instantiate case record this.cas= (case)stdController.getRecord(); //SOQL query cas = [SELECT Type FROM Case where Id =: cas.Id]; //Case record type conditionals If (cas.Type=='AAA') { emailBody = 'AAA body content'; subject = 'AAA subject'; } If (cas.Type=='BBB') { emailBody = 'BBB body content'; subject = 'BBB subject'; } If (cas.Type=='CCC') { emailBody = 'CCC Body'; subject = 'CCC subject'; } } //Accessor methods public string GetEmailBody() { If (emailBody == NULL) { return defaultEmailBody; } Else { return emailBody; } } public string GetSubject() { If (subject == NULL) { return defaultSubject; } Else { return subject; } } }
The test code (currently provides 40% coverage):
@isTest public class testMyPage2 { static testMethod void myPage_Test() { PageReference pageRef = Page.CaseEmailPublisher; Test.setCurrentPageReference(pageRef); //Create new account Account newAccount = new Account (name='XYZ Org'); insert newAccount; //Create first contact Contact myContact = new Contact (FirstName='Joe', LastName='Miller', AccountId=newAccount.id); insert myContact; //Create first case Case myCase = new Case( ContactId=myContact.id, Type='AAA'); insert myCase; ApexPages.StandardController sc = new ApexPages.standardController(myCase); myControllerExtention2 e = new myControllerExtention2(sc); myCase.type='BBB'; update myCase; myCase.type='CCC'; update myCase; } }
Hi,
As Bhawani Sharma told correct one try this.
@isTest(SeeAllData=true)
public class testMyPage2{
static testMethod void myPage_Test() {
PageReference pageRef = Page.CaseEmailPublisher;
Test.setCurrentPageReference(pageRef);
//Create new account
Account newAccount = new Account (name='XYZ Org');
insert newAccount;
//Create first contact
Contact myContact = new Contact (FirstName='Joe',
LastName='Miller',
AccountId=newAccount.id);
insert myContact;
//Create first case
Case myCase = new Case(
ContactId=myContact.id,
Type='AAA');
insert myCase;
ApexPages.StandardController sc = new ApexPages.standardController(myCase);
myControllerExtention2 e = new myControllerExtention2(sc);
myCase.type='BBB';
update myCase;
e = new myControllerExtention2(sc);
myCase.type='CCC';
update myCase;
e = new myControllerExtention2(sc);
e.getEmailBody();
e.getSubject();
}}
Regards,
Rajesh.
All Answers
myCase.type='BBB';
update myCase;
e = new myControllerExtention2(sc);
myCase.type='CCC';
update myCase;
e = new myControllerExtention2(sc);
and after that, call:
e.GetEmailBody();
and
e.GetSubject();
Hi,
As Bhawani Sharma told correct one try this.
@isTest(SeeAllData=true)
public class testMyPage2{
static testMethod void myPage_Test() {
PageReference pageRef = Page.CaseEmailPublisher;
Test.setCurrentPageReference(pageRef);
//Create new account
Account newAccount = new Account (name='XYZ Org');
insert newAccount;
//Create first contact
Contact myContact = new Contact (FirstName='Joe',
LastName='Miller',
AccountId=newAccount.id);
insert myContact;
//Create first case
Case myCase = new Case(
ContactId=myContact.id,
Type='AAA');
insert myCase;
ApexPages.StandardController sc = new ApexPages.standardController(myCase);
myControllerExtention2 e = new myControllerExtention2(sc);
myCase.type='BBB';
update myCase;
e = new myControllerExtention2(sc);
myCase.type='CCC';
update myCase;
e = new myControllerExtention2(sc);
e.getEmailBody();
e.getSubject();
}}
Regards,
Rajesh.
Your're a genius, thanks!