+ Start a Discussion
xpatxpat 

Test public pagereference failed

Code:
public PageReference getRedir() { 
PageReference newPage;
Account account = [Select id, Parentid, Last_Name__c, first_name__c, address_country__c, ownership__c From account Where Id = :ApexPages.currentPage().getParameters().get('id')]; 
if (account.Parentid == null && account.ownership__c == 'JDE' && (account.address_country__c == 'de' ||account.address_country__c == 'fr' || account.address_country__c == 'at'|| account.address_country__c == 'ch' || account.address_country__c == 'li' || account.address_country__c == 'gb' || account.address_country__c == 'gb1' || account.address_country__c == 'ie' || account.address_country__c == 'it'|| account.address_country__c == 'es' || account.address_country__c == 'es1') ) { 
return null;
 } else { 
 newPage = new PageReference('/' + account.id);     
        return newPage.setRedirect(true);            
 }
 }

Hello,

I did test for my controller extension but can't test this part ans of course I don't have my 75 %.

I get an error for the red line

20081222094201.935:Class.yearlyaccountplanController.getRedir: line 9, column 19: SOQL query with 0 rows finished in 4 ms
System.QueryException: List has no rows for assignment to SObject

I thought : "testaccount = [Select id, Parentid, Last_Name__c, first_name__c, address_country__c, ownership__c From account Where name ='XYZ Organization'];   " will solve this issue but not.

Can You help me?

below my test

Code:
static testMethod void Testyearlyex() {           
        date d = date.today();        
        Account testAccount = new Account (name='XYZ Organization',address_country__c='FR',Ownership__c ='JDE',parentid=null);
        insert testAccount;
           ACCOUNT[] getAccountid = [SELECT Id FROM Account WHERE name = 'XYZ Organization'];
    ApexPages.StandardController ac1 = new ApexPages.StandardController(testAccount);
       yearlyaccountplanController yp = new yearlyaccountplanController(ac1);
        testaccount = [Select id, Parentid, Last_Name__c, first_name__c, address_country__c, ownership__c From account Where name ='XYZ Organization'];      
     
       Contact testContact = new Contact (FirstName='Joe',
        LastName='Schmoe',
        AccountId=getAccountId[0].Id);
        insert testContact;
   
        CONTACT[] getContactId = [SELECT Id FROM Contact WHERE AccountId=:getAccountId[0].Id];
       
        Yearly_account_plan__c testplan = new yearly_account_plan__c (name='2008', account__c= getAccountId[0].Id, contact__c = getContactId[0].Id, start_date__c=d);
        insert testplan;
        Yearly_account_plan__c[] getyerId = [SELECT Id FROM yearly_account_plan__c WHERE Account__c=:getAccountId[0].Id];
        
        Practice_management__c testpr = new Practice_management__c (name ='2008',account__c= getAccountId[0].Id);
        insert testpr;
    
        Coupon_requested__c testcp = new Coupon_requested__c (name = 'Case Pack - 80 - FY08',account__c = getAccountId[0].Id,start_date__c=d);
        insert testcp;
  
          yp.getpromo();
          yp.getcon();
          yp.getreq();
          yp.getpr();
       yp.getredir();
      yp.saveall();
       
    }

Thanks

Pat
 


 
aalbertaalbert
I see two possible issues.
1. Since you are returning 0 rows in your query, make sure the currentPage has the "Id" parameter set. To do so, just add a System.debug statement to check the value before the
query. System.debug("Current Page Id: " + ApexPages.currentPage().getParameters().get('id'));

2. Since your query return type is just a single Account object, Apex throws an exception if 0 rows or more than 1 row are returned.
So try changing your code to return an array. For example:

Account[] accounts = [Select id, Parentid, Last_Name__c, first_name__c, address_country__c, ownership__c From account Where Id = :ApexPages.currentPage().getParameters().get('id')];

And then handle the array of accounts returned in the remaining code snippet that access the "accounts" object. 
Here is a link to the apex documentation on querying single records: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/langCon_apex_SOQL_single_row.htm


I hope this helps.


xpatxpat

Thanks for your help, I modify my code and it works :smileyhappy: My code coverage is now 90 %

It's my first page and controller extension, a lot to learn.

Code:
public PageReference getRedir() { 
PageReference newPage;
Account[] accounts = [Select id, name, Parentid, Last_Name__c, first_name__c, address_country__c, ownership__c From account Where Id = :ApexPages.currentPage().getParameters().get('id') and ownership__c = 'JDE'and Parentid = '' and (address_country__c = 'de' or address_country__c = 'fr' or address_country__c = 'at'or address_country__c = 'ch' or address_country__c = 'li' or address_country__c = 'gb' or address_country__c = 'gb1' or address_country__c = 'ie' or address_country__c = 'it'or address_country__c = 'es' or address_country__c = 'es1')]; 
if(accounts.size() == 1){
return null;
 } else { 
 newPage = new PageReference('/' + account.id);     
        return newPage.setRedirect(true);            
 }
 }