Find your content:

Search form

You are here

Getting Test failure for page parameters

 
Share

Getting error message when I'm running the test don't know what's wrong with this code

this is the error msg: System.StringException: Invalid id: recordtype.id

Stack Trace: Class.LeadEventCreation.dosave: line 13, column 1 
             Class.LeadEventCreation.test_BathSolution: line 50, column 1


 public with sharing class LeadEventCreation 
{
    public PageReference dosave() 
    {
        if(!Test.isRunningTest())
        insert objlead;
        objlead.recordTypeId = apexpages.currentPage().getparameters().get('id'); // Line 13,column 1
        if(!Test.isRunningTest())
        update objlead;

        objevent.whoId = objlead.id;
        if(!Test.isRunningTest())
        insert objevent;  
        return (new pagereference('/'+objlead.id).setredirect(true));  
    }
    public Lead objlead { get; set; }
    public Event objevent { get; set; }
    public LeadEventCreation ()
    {
        objlead = new Lead();
        objevent = new Event();
    }

    private static testmethod void test_BathSolution()
    {
        test.startTest();

                    Lead objlead = new lead();
                    objlead.LastName= 'test name';
                    objlead.Response_Channel__c = 'Email';
                    objlead.Status = 'Hoax';
                    objlead.Products__c = 'Shower';
                    objlead.leadSource = 'Website';
                    objlead.Geo_Code__c = 'North';
                    objlead.Product_Type__c='Walk in';
                    objlead.Medium__c = 'All The Best';
                    objlead.DoNotCall = False;
                database.insert(objlead,false);
                RecordType recordtype = [select id,name,sobjecttype from RecordType where sobjecttype = 'Lead' limit 1];
                    objlead.RecordTypeId = recordtype.id;
                database.update(objlead,false);

                LeadEventCreation Bsolution= new LeadEventCreation  ();
                ApexPages.currentPage().getParameters().put('id','recordtype.id');// line 50, Column 1
                Bsolution.doSave();
        test.stoptest();        
   }
}

Attribution to: rakesh

Possible Suggestion/Solution #1

Your insert statements only execute when you are not running a test due to the !Test.isRunningTest() conditional. So, when you run a test the ID field will never get set as well as anything else that happens with an insert will never happen.

I suggest removing those and then do the following in your test method:

  1. Create, but do not insert the records needed in your controller for insert.

  2. Set the controller's properties to those objects, e.g., controller.objLead = newRec, where objLead is a public Lead property.

  3. Remove the !Test.isRunningTest() conditionals.

This will cause the properties to exist when the insert statement inside of your doSave method are called. Also make sure that the API version of your test class is at least 24 (it probably is if you just created it), because API version 24+ has no access to the org's actual data by default.

Unit tests in Salesforce require you to set up the data manually as part of each unit test. See Introduction to Apex test Methods and Unit Testing Custom Controller and Extensions.

Basically, each one of your unit tests should follow the pattern of setup all test data needed, possibly set variables on controllers, call out to controller methods under test within a Test.startTest() and Test.stopTest() block.

// Generic example
static testMethod void testMyUnitTest() {
    // Set up all data here that your controller will expect.
    Account acct = new Account(Name='Unit Test Account');

    // Testing insert inside of a controller, so don't insert the account.

    // Create the controller and then assign the account.
    MyCustomController controller = new MyCustomController();
    controller.account = acct;

    // Alternatively you could have a controller extension...
    ApexPages.StandardController sc = new ApexPages.StandardController(acct);
    MyAccountControllerExtension extension = new MyAccountControllerExtension (acct);

    Test.startTest();

    controller.doSave();

    Test.stopTest(); 
}

I also suggest reading the SFSE tag wiki for unit-test.


Attribution to: Peter Knolle
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4955

My Block Status

My Block Content