0
public class EppExtensionController {

Public Epp__c app{get;set;}
ApexPages.StandardController sc;

Public EppExtensionController(apexpages.standardcontroller stdCon){
sc = stdcon;

app = new Epp__c();
}
public PageReference Save(){


insert app;
if(app.S_Dy__c=='Yes' )
    {

    **PageReference Page = new PageReference('/apex/YesSDy');               
            Page.setRedirect(true);
            return Page;**
         }
   else
    {
    PageReference Page = new PageReference('/apex/NoSDy');

    Page.setRedirect(true);
    return Page;
    }

   }
 }

I cant seem to get this part of the code to cover:

PageReference Page = new PageReference('/apex/YesSDy');
Page.setRedirect(true); return Page;

There is only 76% coverage. I had tried to create two methods in the test class to set app.S_dy to Yes and No.

@isTest

public class EppExtensionControllerTest

{

 static testMethod void testMethod()

 {

     Epp__c App = new Epp__c();

     App.Name='Test' ;
       App.S_dy__c ='No';
    insert App;



 Test.StartTest();


 ApexPages.StandardController sc = new ApexPages.StandardController(app);

 EppExtensionController  ext = new EppExtensionController (sc);
 PageReference pageRef = Page.EppPage; 
 Test.setCurrentPage(pageRef);


    ext.Save();

     Test.StopTest();

} }

  • Your current code only shows S_dy__c being set to "No" which would cause your issue. I don't see an instance in your current test code that would handle both parts of your if statement. – Dan Kowalski Dec 8 '17 at 16:35
  • Epp__c App = new Epp__c(); App.Name='Test' ; App.S_dy__c ='Yes'; insert App; I had added this to it as well but it did not cover. – adming32442 Dec 8 '17 at 16:36
  • How is the original code run? Do you call it from a visualforce page? If so can I see the visualforce code as well. – Dan Kowalski Dec 8 '17 at 16:40
  • Yes it is called from when you save a record on a visualforce page. <apex:commandButton value="Save" action="{!Save}"/> the visualforce page is just a form to fill out, first name, last name, dob, address, city, state. and a picklist asking if they are available (S_dy__c) and then the save button. its still a work in progress i just wanted to see if it will redirect to two VF pages when yes or no is chosen. it works fine, it just isnt covered. – adming32442 Dec 8 '17 at 16:46
0

So from what I see there are two ways to solve your current problem. First you can set the picklist value in the test code so that the value is set or you can update your class so that the object is actually set to the record you are sending the standard controller. I have detailed out each answer below.

Setting the Value in the test class

public class EppExtensionControllerTest

{

 static testMethod void testMethod()

 {

     Epp__c App = new Epp__c();

     App.Name='Test' ;
       App.S_dy__c ='No';
    insert App;



 Test.StartTest();


 ApexPages.StandardController sc = new ApexPages.StandardController(app);

 EppExtensionController  ext = new EppExtensionController (sc);
 PageReference pageRef = Page.EppPage; 
 Test.setCurrentPage(pageRef);


     ext.S_dy__c = "Yes"
     ext.Save();

     Test.StopTest();
} 
}

This insure that you are setting the value in the test class since your controller does not set the app record to anything but a blank value and the visualforce page would take care of setting the values in the fields.

Set the value in the constructor of the controller:

 Public EppExtensionController(apexpages.standardcontroller stdCon){
    sc = stdcon;

    app = new Epp__c();
    this.app = (Epp__c)stdCon.getRecord();

    }

This should also help your issue because you are setting the app to the values that come from the record you are sending to the standard controller.

  • app = new Epp__c() is not necessary here and for consistency, it should be this.sc = stdcon; – cropredy Dec 9 '17 at 6:24
0
 static testMethod void testMethod()

{

 Epp__c App = new Epp__c();

 App.Name='Test' ;

insert App;



Test.StartTest();


ApexPages.StandardController sc = new ApexPages.StandardController(app);

EppExtensionController  ext = new EppExtensionController (sc);
PageReference pageRef = Page.EppPage; 
Test.setCurrentPage(pageRef);


 ext.app.S_dy__c = 'Yes'
 ext.Save();

 Test.StopTest();
} 
static testMethod void testMethod2()

{

 Epp__c App = new Epp__c();

 App.Name='Test' ;

insert App;



Test.StartTest();


 ApexPages.StandardController sc = new ApexPages.StandardController(app);

EppExtensionController  ext = new EppExtensionController (sc);
PageReference pageRef = Page.EppPage; 
Test.setCurrentPage(pageRef);


 ext.app.S_dy__c = 'No'
 ext.Save();

 Test.StopTest();
 } 
 }

The test class above covered 100%. i ended up creating two methods since it was a IF statement and added ext.app.S_dy__c = 'Yes' and ext.app.S_dy__c = 'No'. turned out to be a simple solution. but thanks Dan! im very new to the whole developing side of Salesforce so sorry if the questions are very simple to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.