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
PFangPFang 

How to create a test method for this Apex Class?

Hi guys,

 

I need help with this Apex Class as I'm having difficulties creating a test method for it.

 

public with sharing class myQuestionnaireExtension {
private final Questionnaires__c webquestionnaire;
public myQuestionnaireExtension(ApexPages.StandardController
stdController) {
webquestionnaire = (Questionnaires__c)stdController.getRecord();
}
public PageReference saveQuestionnaire() {
try {
insert(webquestionnaire);
}
catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
PageReference p = Page.ThankYou;
p.setRedirect(true);
return p;
}
}

 

Here's what I've started with:

 

@isTest
private class myQuestionnaireExtensionTest {
    static testMethod void myQuestionnaireExtension() {
        // Set up the Account record.
        Questionnaires__c q = new Questionnaires__c(Name = 'TestQuestionnaire');
        insert q;
        
        }
}

 

Any help on this would be appreciated and be rewarded by a thousand likes! 

 

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh SriramuluRajesh Sriramulu

Hi,

 

 Try this

 

 @isTest
private class myQuestionnaireExtensionTest {   
static testMethod void myQuestionnaireExtension() {
Questionnaires__c q = new Questionnaires__c(Name = 'TestQuestionnaire');
insert q;
ApexPages.StandardController con = new ApexPages.StandardController(q);
myQuestionnaireExtension mqe = new myQuestionnaireExtension(con);
PageReference ref = mqe.saveQuestionnaire();
        } }

 

Regards,

Rajesh.

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi,

 

 Try this

 

 @isTest
private class myQuestionnaireExtensionTest {   
static testMethod void myQuestionnaireExtension() {
Questionnaires__c q = new Questionnaires__c(Name = 'TestQuestionnaire');
insert q;
ApexPages.StandardController con = new ApexPages.StandardController(q);
myQuestionnaireExtension mqe = new myQuestionnaireExtension(con);
PageReference ref = mqe.saveQuestionnaire();
        } }

 

Regards,

Rajesh.

This was selected as the best answer
PFangPFang

Hi Rajesh,

 

Appreciate your help on this a lot, I mean a whole lot!

 

@isTest
private class myQuestionnaireExtensionTest {   
static testMethod void myQuestionnaireExtension() {
Questionnaires__c q = new Questionnaires__c(Name = 'TestQuestionnaire');
insert q;
ApexPages.StandardController con = new ApexPages.StandardController(q);
myQuestionnaireExtension mqe = new myQuestionnaireExtension(con);
PageReference ref = mqe.saveQuestionnaire();
        } }

 

Unfortunately, after trying to run tests on the Apex Class, I still ended up with 0% coverage. Should this be created as a separate Apex Test Class or should I embed this within the code I placed on top?

 

Thanks again in advance!

@anilbathula@@anilbathula@

Hi Pfang

 

Please try the below test method.

By creating a new class.

hope this will work.

 

 

@isTest
private class myQuestionnaireExtensionTest {
public static testMethod void myQuestionnaireExtension() {
// Set up the Account record.
 Questionnaires__c q = new  Questionnaires__c(Name = 'TestQuestionnaire');
insert q;

ApexPages.StandardController sc = new ApexPages.standardController(q);
myQuestionnaireExtension lc=new myQuestionnaireExtension(sc);
lc.saveQuestionnaire();
PageReference Search = new PageReference('Page.Thankyou');
}
}

 

Thanks

Anil.B

Rajesh SriramuluRajesh Sriramulu

Hi

 

New Apex Class.And Run it.

 

Regards,

Rajesh.

PFangPFang

Thanks man! I just ran into certain required field errors but I think I can take it from here...will keep you posted.

PFangPFang

@SRS8

 

Appreciate your patience with a Dev noob! Let me just work on a code for a 'Like' button here on Boards, and you'll get my first click.