I Have this apex class which is called trough a button calling a visualforce page. How do I test my Pagereference in my testclass?
I do not have coverage from // Code we will invoke on page load.
Visualforce:
<apex:page standardController="Contract" extensions="ContractController" action="{!autoRun}">
</apex:page>
Controller
public class ContractController {
// Constructor - this only really matters if the autoRun function doesn't work right
private final Contract c;
public ContractController(ApexPages.StandardController stdController) {
this.c = (Contract)stdController.getRecord();
}
// Code we will invoke on page load.
public PageReference autoRun() {
String theId = ApexPages.currentPage().getParameters().get('id');
if (theId == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}
// Execute code
List<Contract> jaarcontracten = [SELECT id, ContractNumber, startdate FROM Contract WHERE Aantal_maanden__c = 12];
for(Contract c: jaarcontracten ){
c.startdate= Date.newInstance((c.startdate.year() + 1),1,1);
}
update jaarcontracten;
// Redirect the user back to the original page
PageReference pageRef = new PageReference('/' + theId);
pageRef.setRedirect(true);
return pageRef;
}
}
Testclass:
@isTest
public class ContactControllerTest {
@isTest
private static void testContactController()
{
Account acc = new account(name='acc');
insert acc;
Opportunity opp = New Opportunity(accountid = acc.id,closedate=date.today(),name='Test Opport',stagename='//STAGE NAME',Discount_Percent__c=0.2);
insert opp;
list<contact> contacts=new list<contact>();
contacts.add(new contact(AccountId=acc.id,lastname='contact1LastNameA',firstname='Contact2FirstNameB',Lidnummer__c='9876543211',LeadSource='web',Splits__c=false));
Insert Contacts;
//Create the contract
Contract con = new Contract(AccountId = opp.AccountId,ContractTerm = 12, StartDate = opp.CloseDate,OwnerExpirationNotice = '30');
insert con;
ApexPages.StandardController sc = new ApexPages.standardController(con);
ContractController myPageCon = new ContractController(sc);
PageReference pageRef = Page.VerlengingContract;
pageRef.getParameters().put('id', String.valueOf(Contract.Id));
Test.setCurrentPage(pageRef);
}
}