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
subramanyam subbu 7subramanyam subbu 7 

how to write test class for this method

 public Pagereference saveQuestion(){
        businessfetchedDataToSave = [Select Id,Name ,Subject_Matter_Expert__r.Name ,Subject_Matter_Expert__r.Email from Line_Of_Business__c where Name =:lineDisciplineValue];
        Question__c q = new Question__c ();
        q.Line_Of_Discipline_C__c = businessfetchedDataToSave.Id;
        User u = [select id from user where user.Name=:businessfetchedDataToSave.Subject_Matter_Expert__r.Name];
        q.Line_Of_Discipline_Focal_Point__c = u.id;
        q.Line_Of_Discipline_Focal_Point_Email__c = businessfetchedDataToSave.Subject_Matter_Expert__r.Email;
        q.Priority_C__c = ques.Priority_C__c;
        q.Title_C__c = ques.Title_C__c;
        q.Question_C__c = ques.Question_C__c;
        q.Status_C__c = 'Open';
        q.Site__c = companySiteValue;
        insert q;    
        
        return null;
    }
James LoghryJames Loghry

Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
Starting the test. This is calling Test.startTest() to reset the governor limits
Calling your class / method
Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
Asserting that your changes have worked

​If you have inserted/updated/deleted data, you need to query for the updates
Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data backIf you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
subramanyam subbu 7subramanyam subbu 7
my apex classs..........

public with sharing class extController {

    public Document document { get; set; }
   
    public Line_Of_Business__c businessfetchedData{get; set;}
    public Line_Of_Business__c businessfetchedDataToSave{get; set;}
    public string lineDisciplineValue{get; set;}
    public Question__c ques {get; set;}-
   
    public extController() {
        ques = new Question__c();
    }
   
   
   
    public List<SelectOption> getItems() {
        List<Line_Of_Business__c> getBusinessRecs  =  new List<Line_Of_Business__c>([Select Id,Name from Line_Of_Business__c limit 50000]);
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--None--'));
        for(Line_Of_Business__c business : getBusinessRecs){
            options.add(new SelectOption(business.Name,business.Name));
        }
       
       
        return options;
    }
   
    public Pagereference getRelatedValues(){
        string businessLine = ApexPages.currentPage().getParameters().get('businessValue');
        businessfetchedData = [Select Id,Name ,Subject_Matter_Expert__r.Name ,Subject_Matter_Expert__r.Email from Line_Of_Business__c where Name =:businessLine];
        return null;
    }
   
    public Pagereference saveQuestion(){
        businessfetchedDataToSave = [Select Id,Name ,Subject_Matter_Expert__r.Name ,Subject_Matter_Expert__r.Email from Line_Of_Business__c where Name =:lineDisciplineValue];
        Question__c q = new Question__c ();
        q.Line_Of_Discipline_C__c = businessfetchedDataToSave.Id;
        User u = [select id from user where user.Name=:businessfetchedDataToSave.Subject_Matter_Expert__r.Name];
        q.Line_Of_Discipline_Focal_Point__c = u.id;
        q.Line_Of_Discipline_Focal_Point_Email__c = businessfetchedDataToSave.Subject_Matter_Expert__r.Email;
        q.Priority_C__c = ques.Priority_C__c;
        q.Title_C__c = ques.Title_C__c;
        q.Question_C__c = ques.Question_C__c;
        q.Status_C__c = 'Open';
        insert q;
       
        return null;
    }

my test class.....testclass was failing not getting green tick mark...................


@isTest
public class testcontroller
{
static testmethod void testingex()
{
Account ad=new Account(Name='rahul',Phone='8989899');
insert ad;
Opportunity op=new Opportunity(Name='gandhi',CloseDate=System.today(), StageName='prospecting');
insert op;
Line_Of_Business__c lb=new Line_Of_Business__c(name='rajendra');
insert lb;
Document d=new Document();
    
Test.starttest();
PageReference pageRef = Page.sccquestionFormSubmission;
pageRef.getParameters().put('id', String.valueOf(lb.Id));
Test.setCurrentPage(pageRef);
extController q = new extController();
q.lineDisciplineValue='business';
q.companySiteValue='business1';
List<SelectOption> so = q.getItems();

so=q.getSites();
q.savequestion();
   
//so=qd.getItems();
Test.stopTest();
    System.assertEquals(null,q.lineDisciplineValue);
    System.assertEquals(null,q.companySiteValue);
 
    
}
static testmethod void testingexx()
{
Account ac=new Account(Name='rahul',Phone='8989899');
insert ac;
Opportunity opp=new Opportunity(Name='gandhi',CloseDate=System.today(), StageName='prospecting');
insert opp;
question__c qd=new   question__c(Status_C__c = 'Open', Site__c = 'companySiteValue');
insert qd;
    
Test.starttest();
extController tes = new extController();
PageReference Ref = Page.sccquestionFormSubmission;
Ref.getParameters().put('id', String.valueOf(qd.Id));
Test.setCurrentPage(Ref);
tes.getRelatedValues();
    
Test.stopTest();
}
static testmethod void testingexxx()
{
 extController tes = new extController();
    tes.savequestion();
 Question__c que = new Question__c ();
    que.Status_C__c = 'open' ;
    insert que;
}
 
   
}