You need to sign in to do that
Don't have an account?
Lam Corporation
Dear Controller Extensions Experts! Can you get this simple code from 80% to 100%?
Dear all,
I have a Apex Controller Extension and a Test Class all for a Visualforce Page. I am able to get 80% code coverage. I just can't seem to cover 3 lines to do with the page redirect (highlighted in Bold and Italic).
Apex Controller Extension
public with sharing class MYExtensionController {
private ApexPages.StandardController sc;
public MYExtensionController(ApexPages.StandardController sc) {
this.sc = sc;
}
public PageReference save() {
My_Object__c s = (My_Object__c) sc.getRecord();
try{
My_Object__c so = new My_Object__c();
so = (My_Object__c)sc.getRecord();
so.My_Picklist__c = 'My Picklist Value';
insert s;
pagereference ref = new pageReference('/apex/ThankYou');
ref.setRedirect(true);
return ref;
}
catch(Exception ex){
ApexPages.addMessages(ex);
}
return null;
}
}
Apex Test Class
@isTest
public class MYExtensionControllerTest {
public static testMethod void testMyController() {
PageReference pageRef = Page.ThankYou;
Test.setCurrentPage(pageRef);
// Instanstiate new sObject record
My_Object__c wo = new My_Object__c();
// Populate the required values
wo.Location__c = 'My House';
wo.Date__c = Date.today();
wo.Observation__c = 'Breakdown;
wo.Action__c = 'Did Something';
wo.Location_Description__c = 'Location22';
wo.My_Picklist__c = 'My Picklist Value';
insert wo;
ApexPages.StandardController sc = new ApexPages.StandardController(wo);
MYExtensionController a = new MYExtensionController(sc);
Test.startTest();
a.save();
System.assertEquals(pageRef.getUrl(),'/apex/thankyou');
My_Object__c[] wo1 = [select id, name, My_Picklist__c from My_Object__c where Location_Description__c = 'Location22'];
System.assertEquals('My Picklist Value', wo1[0].My_Picklist__c);
Test.stopTest();
}
}
I have a Apex Controller Extension and a Test Class all for a Visualforce Page. I am able to get 80% code coverage. I just can't seem to cover 3 lines to do with the page redirect (highlighted in Bold and Italic).
Apex Controller Extension
public with sharing class MYExtensionController {
private ApexPages.StandardController sc;
public MYExtensionController(ApexPages.StandardController sc) {
this.sc = sc;
}
public PageReference save() {
My_Object__c s = (My_Object__c) sc.getRecord();
try{
My_Object__c so = new My_Object__c();
so = (My_Object__c)sc.getRecord();
so.My_Picklist__c = 'My Picklist Value';
insert s;
pagereference ref = new pageReference('/apex/ThankYou');
ref.setRedirect(true);
return ref;
}
catch(Exception ex){
ApexPages.addMessages(ex);
}
return null;
}
}
Apex Test Class
@isTest
public class MYExtensionControllerTest {
public static testMethod void testMyController() {
PageReference pageRef = Page.ThankYou;
Test.setCurrentPage(pageRef);
// Instanstiate new sObject record
My_Object__c wo = new My_Object__c();
// Populate the required values
wo.Location__c = 'My House';
wo.Date__c = Date.today();
wo.Observation__c = 'Breakdown;
wo.Action__c = 'Did Something';
wo.Location_Description__c = 'Location22';
wo.My_Picklist__c = 'My Picklist Value';
insert wo;
ApexPages.StandardController sc = new ApexPages.StandardController(wo);
MYExtensionController a = new MYExtensionController(sc);
Test.startTest();
a.save();
System.assertEquals(pageRef.getUrl(),'/apex/thankyou');
My_Object__c[] wo1 = [select id, name, My_Picklist__c from My_Object__c where Location_Description__c = 'Location22'];
System.assertEquals('My Picklist Value', wo1[0].My_Picklist__c);
Test.stopTest();
}
}
Your insert operation on My_Object__c (insert s) is failing and hence you are not able to get those 3 lines tested.
You are basically trying to insert a record which is already inserted in the database.
All Answers
Your insert operation on My_Object__c (insert s) is failing and hence you are not able to get those 3 lines tested.
You are basically trying to insert a record which is already inserted in the database.
Worked a treat. So obvious when its pointed out.
I removed the insert statement and added a couple of lines including an assertion. (Marked in bold / italic)
@isTest
public class MYExtensionControllerTest {
public static testMethod void testMyController() {
// Set up Redirect Page Reference
PageReference pageRef = Page.ThankYou;
Test.setCurrentPage(pageRef);
// Instanstiate new sObject record
My_Object__c wo = new My_Object__c();
// Populate the required values
wo.Location__c = 'My House';
wo.Date__c = Date.today();
wo.Observation__c = 'Breakdown;
wo.Action__c = 'Did Something';
wo.Location_Description__c = 'Location22';
wo.My_Picklist__c = 'My Picklist Value';
insert wo;// Instanstiate Controller
ApexPages.StandardController sc = new ApexPages.StandardController(wo);
MYExtensionController a = new MYExtensionController(sc);
// Get string of URL on Save
String nextPage = a.save().getUrl();
Test.startTest();
// Initiate Save/Commit to the Database
a.save();
// Assertions
System.assertEquals('/apex/ThankYou',nextPage);
System.assertEquals(pageRef.getUrl(),'/apex/thankyou');
My_Object__c[] wo1 = [select id, name, My_Picklist__c from My_Object__c where Location_Description__c = 'Location22'];
System.assertEquals('My Picklist Value', wo1[0].My_Picklist__c);
Test.stopTest();
}
}