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
Sri Hari 45Sri Hari 45 

how to create dummy test data in visualforce page..?

Hi..,
I have a apex class in that i'm getting session form visualforce page, for that class i should have to write a test class

I'm using the pagereference  ad it's covering the my class 61%..

So could you please how to create a dummy test data for a visualforce page in Test class..

 

Thanks..,

Sri Hari

Satish PrajapatSatish Prajapat

Hello sri hari 45,
I think your focus should to cover the Apex code not for the VF page, still I am sharing some code please check it:

@isTest
private class testMyController
{
    private static testMethod void testAutoRun() {
        
        test.startTest();
        
        PageReference pageRef = Page.yourPageName;
        Account acc = new Account(Name='Abce');
        insert acc;
        
        Opportunity  testOppty = new Opportunity();
        testOppty.name='testOppty';
        testOppty.AccountId=acc.id;
        testOppty.StageName='Open';
        testOppty.CloseDate=System.today();
        insert testOppty;
        
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id',testOppty.id);
        ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
        
        Myclass  controller = new Myclass(sc);
        System.assertNotEquals(null,controller.autoRun());
        
        test.stopTest();
    }
}


I hope this is helful for you.

Still you are getting difficulties please, share your problems.

Thanks,

Satish.

Sri Hari 45Sri Hari 45

I have class but here in this getContent(), it's not covering my testclass 
global static String getSessionIdFromVFPage(PageReference visualforcePage){
 String content = visualforcePage.getContent().toString();
                Map<String,Object> sessionJSON = (Map<String, Object>) JSON.deserializeUntyped(content);
                String sessionID = String.valueOf(sessionJSON.get('sessionId'));
}

So could you please how to cover that content line in my test class..

Thanks

Satish PrajapatSatish Prajapat

Hi sri Hari 45,
Its easy to cover the code,
please follow the below steps:

  • First create pageReference object (the page which you are passing into getContent page.)
  • Set all parameter into the page (if any like Id etc).
  • Than call the method in test class by passing the pageReference object as a parameter.
@isTest
private class testMyController
{
    private static testMethod void testAutoRun() {
        
        test.startTest();
        
        PageReference pageRef = Page.yourPageName;
        pageRef.getParameters().put('id',Id_value);
        
        Test.setCurrentPage(pageRef);
        Myclass obj = new Myclass();
        obj.getContent(pageRef);
        test.stopTest();
    }
}

If this code help you than choose it as best answer, 
still if you are getting error than let me know, I am ready to help you.

Thanks,
Satish.