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
TehNrdTehNrd 

Question about Unit Testing and setting up currentPageReference()

Whoohooo. I have finally built out a Visual Force page that is ready for prime time but now I need to write the test classes for it.

Here is my stumbling block:

The page is launched from a Page Button on the Lead object ( /apex/mergeLeadToOpp?id={!Lead.Id} )

In the controller the following query is run that uses "System.currentPageReference().getParameters().get('id')"

mylead = [select Id, Name, etc, etc from Lead where ID = :System.currentPageReference().getParameters().get('id')];

Here is my test class (not even close to done) but I can't seem to get it to work. Any help is greatly appreciated.
Code:
public class mergeLeadToOppVFTEST {

  public static testMethod void testMyController() {
    
    Lead l = new Lead(
     Reseller_First_Name__c = 'Jason',
      Reseller_Last_Name__c = 'V',
      Reseller_Email__c = 'jv@f5.com',
      Reseller_Company_Name__c = 'VenTech',
      Reseller_Phone__c = '207-69H-8540',
      Distributor__c = 'Super Disiti',
      Software_Vendor__c = 'SFDC',
      Source_code__c = 'abcd'
    );
    
    insert l;
    
    //I think this is where my problem is. Here is what I have tried but with no luck
    Test.setCurrentPageReference(new PageReference('/apex/mergeLeadToOpp—id=' + l.id));
    Test.setCurrentPageReference(new PageReference('Page.mergeLeadToOpp–id=' + l.id));
    Test.setCurrentPageReference(new PageReference('Page.mergeLeadToOpp')); 
       
    mergeLeadToOppVF controller = new mergeLeadToOppVF();
    
    controller.settargetOpp('asdfasd');
    controller.mergeLead();
    
    String error = controller.getError();
    System.AssertEquals(error,'Link or ID must be at least 15 characters long.');    
  }
}

 



Message Edited by TehNrd on 04-19-2008 10:12 PM
Ron HessRon Hess
Here is an example of adding an ID paramater on a controller under test., i think this will work


Code:

Test.setCurrentPageReference(new PageReference('Page.mergeLeadToOpp'));
System.currentPageReference().getParameters().put('id', l.id);

 

 

TehNrdTehNrd
Ron to the rescue. That is probably right but I get this:

Error: Compile Error: Method does not exist or incorrect signature: Test.setCurrentPageReference(System.PageReference)

but I think there is something wrong with this sandbox. Anything that refers to the Test class is throwing errors. If I try to run others tests on triggers I have built in the past it says: "No testMethods found in selected Apex code" but there are testMethods and in other sandboxes these have worked fine for months. Also if I try to edit something with this "Test.startTest()" it says:

Error: Compile Error: Method does not exist or incorrect signature: Test.startTest() at line 46 column 17

Ehh, I'll refresh the sandbox on Monday, see it this resolves the issues, and report back.



Message Edited by TehNrd on 04-18-2008 09:36 PM
TehNrdTehNrd
Refreshing the sandbox fixed the Test issue and your code did the trick.

Thanks,
Jason
Scott.MScott.M
I just ran into the same problem with one of my sandboxes. I think it might be related to the recent upgrades. The sandbox was working not to long ago. I will refresh and hopefully that works. I just need to back up some of the code I had in development before I do the refresh. Wondering if it's a known issue?
Scott.MScott.M
I just finished refreshing the Sandbox and I'm still seeing the same error for the test function. The code works in other organizations though.

Update:

Tried Changing
 
Code:
Test.setCurrentPageReference(Page.myPage);

 To:
 
Code:
PageReference pageRef = Page.MyPage;
Test.setCurrentPage(pageRef);

 Still getting the error:

Severity and Description    Path    Resource    Location    Creation Time    Id
Save error: Method does not exist or incorrect signature: Test.setCurrentPage(System.PageReference)    hucks_development/src/unpackaged/classes    myExtTests.cls    line 5    1214842167843    51681



Message Edited by Scott.M on 06-30-2008 09:12 AM
LegerdemainLegerdemain

Jason,

 

Do you have a class named "Test" and hence have issues when trying Test.startTest or some other test method?

 

I belive this could be causing some problems when you mention that Test.startTest works only on some sandboxes and only during the upgrade period.

 

Cheers,

Larry

TehNrdTehNrd
Doh! That was it. Thanks!
ThomasTTThomasTT
Thanks, I did the same mistake:D
vshindevshinde

That was exactly what i had done. Thanks for your post.

James LoghryJames Loghry

I was struggling with this as well.

 

I looked at my classes and sure enough there was a Test.cls.

 

Mucho thanks!

Scott Peterson 25Scott Peterson 25
I am having this same problem; however, the class named "Test" is part of a Managed Package that I can not delete as it is critical to our team. Is there anyway around this problem, or do I have to wait for that company to updated the package?