function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mmaxtrammaxtra 

testmethod for retURL???please help

Hi:

   Does anyone know how to write a testmethod for retURL and redirect...

See my code below:

 

public class newoppbutton { String retURL; String oppId; public newoppbutton(ApexPages.StandardController stdController){ } public PageReference init() { retURL= ApexPages.currentPage().getParameters().get('retURL'); oppId = retURL.substring(1); Contact c = [Select lastname,firstname from Contact where id =:oppId]; String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; } else { redirectUrl = '/006/e?CF00N70000002RNNE='+c.firstname+'+'+c.lastname+'&nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } } }

 VF Code:

<apex:page standardController="Opportunity" extensions="newoppbutton" action="{!init}"> </apex:page>

 Thank you

 

 

 

mtbclimbermtbclimber

Not sure what element of this you are trying to test but if you are trying to assert the correct url is returned from the init method depending on a given request parameter then the element you may be missing here is the Test.setCurrentPage(ApexPages.PageReference p) method which allows you to set the expected parameters in the mock request context.

 

Aside from that it appears as though you are dependent on standard page request/form parameters which are not part of any supported API/Contract and are subject to change at any time.  Unfortunately there is no way for you to utilize the Apex test framework to automate the verification that these haven't changed from release to release so if you want to build against this you should, at a minimum, manually test this every major release of salesforce or automate using an external testing tool.

 

mmaxtrammaxtra

Hi Thanks for responding back...

I did start on a testmethod but am getting 21% coverage...

I am failing on a null reference error and do not know exactly how to tackle this...

 

public class newoppbuttontest { public static testMethod void testnewoppbutton() { Account a = new Account ( name='mma', Active__c=True, BillingState='CT', BillingStreet='134 Laurel', BillingCity='haha', BillingPostalCode='11501', BillingCountry='USA', ShippingState = 'CT', ShippingCountry = 'USA', ShippingPostalCode = '11501', ShippingCity = 'haha', ShippingStreet = '134 Laurel' ); insert a; /*CREATE NEW CONTACT*/ Contact c = new Contact(); c.LastName='xxxxxx'; c.firstname='mma'; c.AccountId=a.id; c.Status__c='Active'; c.MailingCountry='US'; c.MailingState='CT'; c.MailingStreet='134 Laurel'; c.MailingCity='hah'; c.MailingPostalCode='11501'; insert c; Contact cn = [select LastName,id from Contact where Id =:c.id]; ApexPages.StandardController controller = new ApexPages.StandardController(new Opportunity()); PageReference customPage = new PageReference('/apex/newbuttonpage'); customPage.getParameters().put('id', cc.name); //System.currentPagereference().getParameters().put('name',cc.LastName); newoppbutton extension = new newoppbutton(controller); //Keep failing here on Null attempt error extension.init(); /* ApexPages.standardController controller = new ApexPages.standardController(new Opportunity()); ApexPages.currentPage().getParameters().put('name', cc.LastName); newoppbutton extension = new newoppbutton(controller); extension.init(); */ } }

 Any Help would be apprecited...

Thanks

 

 

mtbclimbermtbclimber

Well, I don't see how the constructor could be hitting an NPE but I do see how the init method would since it is currently dependent on the page request having a parameter named "retURL" having a value with a lenght of at least 1. And nowhere are you establishing such a parameter in your test.

 

mmaxtrammaxtra

yes true... But I can not figure out how to do that part...

Do you or would you provide a sample of it. Please...

 

Thank you

mtbclimbermtbclimber

With a little searching I found the post marked as the solution to this thread contains a relevant example:

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=18295

 

Message Edited by mtbclimber on 12-15-2009 08:43 PM
MMA_FORCEMMA_FORCE

Hi Thanks...

I am trying basically the same thing here for what the link had but still am getting a dereference null object attempt...

So it can be that... I am still hitting 21%

So any other idea..??

 

Thanks

 

MMA_FORCEMMA_FORCE
Anyone???Pleaseeeeeeeee
MMA_FORCEMMA_FORCE

String redir; redir='/006/e?&nooverride=1'; PageReference p2 = new PageReference(redir); system.debug('PageReference p2:' + p2); Test.setCurrentPage(p2); ApexPages.currentPage().getParameters().put('CF00N70000002RNNE', c.lastname+'+'+c.firstname); system.debug('PageReference p21:' + p2); ApexPages.standardController sc = new ApexPages.standardController(new Opportunity()); newoppbutton controller = new newoppbutton (sc); controller.init();

 So I added this and I still get the error in my test class....

Someone pleaseeeeeeeeeeeeeeeee tell me what I am doing wrong...

Why is it coming up with 

System.NullPointerException: Attempt to de-reference a null object

on the 

newoppbutton controller = new newoppbutton (sc);
controller.init(); 

 

mtbclimbermtbclimber

Because, as I mentioned previously and assuming the code in your first message above is what you are trying to test, your init method is dependent on a request param returning a value which you are not setting in your test:

 

retURL= ApexPages.currentPage().getParameters().get('retURL');
oppId = retURL.substring(1);

 

If retURL is null you are going to hit an NPE (as you are now). If it is non-null and empty you'll get a string exception for providing an index value greater than the length.

 

You need to set a param value that satisfies your init method or change your init method. Either way you're going to need to set the page reference in the test context before calling the init method in your test otherwise it won't find your value and you'll continue to hit the NPE even if you do add the parameter with the right key to the map on your page reference.

MMA_FORCEMMA_FORCE

Hi true...

I took your suggestion and  set page reference before the init method and it fails with the NPE..

 

String redir; redir='/006/e?&nooverride=1'; PageReference p2 = new PageReference(redir); system.debug('PageReference p2:' + p2); ApexPages.currentPage().getParameters().put('CF00N70000002RNNE', c.lastname+'+'+c.firstname);<==setting it here system.debug('PageReference p21:' + p2); Test.setCurrentPage(p2);calling the page here PageReference p2r = Page.newoppbuttonpage; ApexPages.standardController sc = new ApexPages.standardController(new Opportunity()); newoppbutton controller = new newoppbutton(sc);<== this is where it is failing Test.setCurrentPage(p2r);

 

 

 

mtbclimbermtbclimber
Is the code in your init method looking for a parameter named 'nooverride'? As that's the only one that it's going to see based on your test code.
Message Edited by mtbclimber on 12-16-2009 12:42 PM
MMA_FORCEMMA_FORCE

Hi now I have changed the class to:

 

public class newoppbutton { Contact mycontact{get;set;} boolean gotcontact=false; public newoppbutton(ApexPages.StandardController stdController){ string ret=ApexPages.currentPage().getParameters().get('RetURL'); ret=ret.substring(1,ret.length()); if(ret.startswith('003')){ try{ mycontact = [select id,name from contact where id=:ret]; gotcontact=true; }catch(QueryException q){ system.debug(q.getmessage()); } } } public PageReference init() { String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; }else if(gotcontact) { redirectUrl = '/006/e?CF00N70000002RNNE='+mycontact.Name+'&nooverride=1'; }else{ redirectUrl = '/006/e?nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } }

 so the init is not depended on it...

but still get the same error???

 

 

 

mtbclimbermtbclimber

Right, you are just going to get it earlier now because you didn't remove the code that caused the issue, you just moved it to the constructor.

 

This is the code that is causing the error:

 

string ret=ApexPages.currentPage().getParameters().get('RetURL'); ret=ret.substring(1,ret.length());

 

Either remove that code or set a value for the retURL on the pagereference you pass to Test.setCurrentPage() (before you construct the class or call init)

 

mmaxtrammaxtra

ok I can't remove the code because nothing else would work...

but do you mean:

In my Apex class:

instead of:

 

public PageReference init() {

 I do this:

 

public PageReference init(string cc) {

 Then in my test class I do :

 

String redir; redir='/006/e?&nooverride=1'; PageReference p2 = new PageReference(redir); system.debug('PageReference p2:' + p2); ApexPages.currentPage().getParameters().put('CF00N70000002RNNE', c.lastname+'+'+c.firstname); system.debug('PageReference p21:' + p2); PageReference p2r = Page.newoppbutton; ApexPages.standardController sc = new ApexPages.standardController(new Opportunity()); Test.setCurrentPage(p2); Test.setCurrentPage(p2r); newoppbutton controller = new newoppbutton (sc); controller.init(c.id);

 

 Is that correct?

Because i got the same error... maybe i misunderstood..

 

 

 

 

 

mmaxtrammaxtra

Someone pleaseeeee Help me figure out my testmethod ...

I have tried over and over and can not get passed the attempt to null reference...

I do not know what else to do.. Totally at a Lost...

My last try was the following:

I got the contact id but then after that I still get the error...

 

//Now get the contact id redir='/'+ cc.id; PageReference p2 = new PageReference(redir); system.debug('PageReference p21:' + p2); Test.setCurrentPage(p2); string ret; String redire; ret=redir.substring(1); system.debug('PageReference p21d:' + ret); Contact cn=[Select name, id, firstname, lastname,recordtype.name from Contact where id=:ret];

 thank you happy holidays...