You need to sign in to do that
Don't have an account?
Amit_Trivedi
Error in test class
Hi all,
While writing test class for controller i am geting error list index out of bounds though i have satisfied criteria. please help me to get full code coverage. here is my test class
@isTest
public with sharing class SResponseTest
{
public static testMethod void surveyRes()
{
Account acc1 = TestDataGenerator.createAccount('Acme');
insert acc1;
Opportunity opp1 = TestDataGenerator.createOpportunity('TM',acc1,Date.today(),'Closed Won');
insert opp1;
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='A.trivedi@demoorg.com',
EmailEncodingKey='UTF-8', LastName='devindrop', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='KFCempress31@Nagpurorg.com');
insert u;
System.runAs(u)
{
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());
}
Survey__c sur1 = TestDataGenerator.createSurvey('Test','For testing',Date.today(),opp1);
insert sur1;
Question__c que1 = TestDataGenerator.createQuestion('Who',false,'Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Radio',sur1);
insert que1;
Feedback__c fb1 = TestDataGenerator.createFedback('Ans',que1,'Test','Test','Test','Test','Test','Test','Test','Test','Test','Test',u);
insert fb1;
Test.startTest();
ApexPages.CurrentPage().getparameters().put('sid', sur1.id);
ApexPages.StandardController controller = new ApexPages.StandardController(sur1);
SResponse st = new SResponse(controller);
SResponse st1 = new SResponse();
st.index = 1;
// st.queWraList[0].question.id = que1.id;
st.queWraList[0].question.Question_Type__c ='Radio';
st.queWraList[0].question.Is_Mandatory__c = true;
st.queWraList[0].question.Option_1__c = 'test';
st.queWraList[0].question.Option_2__c = 'test';
st.recordCount = st.queWraList.size();
st.qw = st.queWraList[0];
st.index =st.index+ 1;
st.init();
st.getNext();
st.getPrevious();
st.finish();
st.saveFeedback();
st.restrictMand();
Test.stopTest();
}
}
While writing test class for controller i am geting error list index out of bounds though i have satisfied criteria. please help me to get full code coverage. here is my test class
@isTest
public with sharing class SResponseTest
{
public static testMethod void surveyRes()
{
Account acc1 = TestDataGenerator.createAccount('Acme');
insert acc1;
Opportunity opp1 = TestDataGenerator.createOpportunity('TM',acc1,Date.today(),'Closed Won');
insert opp1;
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='A.trivedi@demoorg.com',
EmailEncodingKey='UTF-8', LastName='devindrop', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='KFCempress31@Nagpurorg.com');
insert u;
System.runAs(u)
{
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());
}
Survey__c sur1 = TestDataGenerator.createSurvey('Test','For testing',Date.today(),opp1);
insert sur1;
Question__c que1 = TestDataGenerator.createQuestion('Who',false,'Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Radio',sur1);
insert que1;
Feedback__c fb1 = TestDataGenerator.createFedback('Ans',que1,'Test','Test','Test','Test','Test','Test','Test','Test','Test','Test',u);
insert fb1;
Test.startTest();
ApexPages.CurrentPage().getparameters().put('sid', sur1.id);
ApexPages.StandardController controller = new ApexPages.StandardController(sur1);
SResponse st = new SResponse(controller);
SResponse st1 = new SResponse();
st.index = 1;
// st.queWraList[0].question.id = que1.id;
st.queWraList[0].question.Question_Type__c ='Radio';
st.queWraList[0].question.Is_Mandatory__c = true;
st.queWraList[0].question.Option_1__c = 'test';
st.queWraList[0].question.Option_2__c = 'test';
st.recordCount = st.queWraList.size();
st.qw = st.queWraList[0];
st.index =st.index+ 1;
st.init();
st.getNext();
st.getPrevious();
st.finish();
st.saveFeedback();
st.restrictMand();
Test.stopTest();
}
}
I am asssming that you are getting error at line st.qw = st.queWraList[0];
if it is not can you post your error line number
public with sharing class SResponse
{
public Survey__c survey{get;set;}
public List<QuestionWrapper> queWraList{get;set;}
public List<OptionWrapper> lstOptions{get;set;}
public QuestionWrapper qw{get;set;}
Public Question__c que{get;set;}
public Integer rowIndex { get; set; }
public Integer queNo { get; set; }
Public List<Survey__c> surveyList{get;set;}
Public List<Question__c> queList{get;set;}
Public Feedback__c feedback{get;set;}
Public List<Feedback__c> feedbackList{get;set;}
Public String SurveyId{get;set;}
public Integer recordCount{get;set;}
public Integer index{get;set;}
public Integer percentComplete{get;set;}
public Integer totalPercent{get;set;}
public SResponse(ApexPages.StandardController controller)
{
init();
}
public SResponse()
{
init();
}
public void init()
{
SurveyId = ApexPages.currentPage().getParameters().get('sid');
queWraList= new List<QuestionWrapper>();
feedbackList = new List<Feedback__c>();
que = new Question__c();
survey = new Survey__c();
feedback = new Feedback__c();
lstOptions = new List<OptionWrapper>();
surveyList =[SELECT Id,Name,Description__c FROM Survey__c WHERE Id=:SurveyId ];
qw = new QuestionWrapper(que);
index = 0;
if(surveyList.size()>0)
{
survey = surveyList[0];
}
if(survey.Id!=null)
{
queList = new List<Question__c>();
queList = [SELECT Id,Question__c,Question_Type__c,Option_1__c,Option_2__c,Option_3__c,Option_4__c,Option_5__c,Option_6__c,
Option_7__c,Option_8__c ,Option_9__c ,Is_Mandatory__c ,Option_10__c,Answer__c FROM Question__c WHERE Survey__c=:survey.id Limit 1000 ];
}
if(queList != null && queList.size()>0)
{
queWraList= new List<QuestionWrapper>();
for(Question__c que : queList )
{
QuestionWrapper qw = new QuestionWrapper (que);
qw.srNo = queWraList.size()+1;
queWraList.add(qw);
}
}
recordCount = queWraList.size();
if(queWraList.size()>0)
{
qw = queWraList[index];
}
}
public void getNext()
{
saveFeedback();
restrictMand();
}
public void getPrevious()
{
index--;
qw = queWraList[index];
}
public void finish()
{
saveFeedback();
index++;
try
{
if(feedbackList.size()>0)
{
upsert feedbackList;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Your feedback successfully submitted'));
}
}
catch(Exception e)
{
throw e;
}
}
public void saveFeedback()
{
if(qw.question.Question_Type__c=='Descriptive')
{
Feedback__c fb = new Feedback__c();
fb.Question__c = qw.question.Id;
fb.Answer_Descriptive__c = qw.question.Answer__c;
feedbackList.add(fb);
}
if(qw.question.Question_Type__c=='Checkbox')
{
Feedback__c fb = new Feedback__c();
fb.Question__c = qw.question.Id;
if(qw.lstOptions.size()>=1 && qw.lstOptions[0].selectedanscheck==true)
{
fb.Option1__c= qw.lstOptions[0].opt;
}
if(qw.lstOptions.size()>=2 && qw.lstOptions[1].selectedanscheck==true)
{
fb.Option2__c= qw.lstOptions[1].opt;
}
if(qw.lstOptions.size()>=3 && qw.lstOptions[2].selectedanscheck==true)
{
fb.Option3__c= qw.lstOptions[2].opt;
}
if(qw.lstOptions.size()>=4 && qw.lstOptions[3].selectedanscheck==true)
{
fb.Option4__c= qw.lstOptions[3].opt;
}
if(qw.lstOptions.size()>=5 && qw.lstOptions[4].selectedanscheck==true)
{
fb.Option5__c= qw.lstOptions[4].opt;
}
if(qw.lstOptions.size()>=6 && qw.lstOptions[5].selectedanscheck==true)
{
fb.Option6__c= qw.lstOptions[5].opt;
}
if(qw.lstOptions.size()>=7 && qw.lstOptions[6].selectedanscheck==true)
{
fb.Option7__c= qw.lstOptions[6].opt;
}
if(qw.lstOptions.size()>=8 && qw.lstOptions[7].selectedanscheck==true)
{
fb.Option8__c= qw.lstOptions[7].opt;
}
if(qw.lstOptions.size()>=9 && qw.lstOptions[8].selectedanscheck==true)
{
fb.Option9__c= qw.lstOptions[8].opt;
}
if(qw.lstOptions.size()>=10 && qw.lstOptions[9].selectedanscheck==true)
{
fb.Option10__c= qw.lstOptions[9].opt;
}
feedbackList.add(fb);
}
if(qw.question.Question_Type__c=='Radio')
{
Feedback__c fb = new Feedback__c();
fb.Question__c = qw.question.Id;
if(qw.question.Option_1__c==qw.selectedRadio)
{
fb.Option1__c= qw.question.Option_1__c;
}
if(qw.question.Option_2__c==qw.selectedRadio)
{
fb.Option2__c= qw.question.Option_2__c;
}
if(qw.question.Option_3__c==qw.selectedRadio)
{
fb.Option3__c= qw.question.Option_3__c;
}
if(qw.question.Option_4__c==qw.selectedRadio)
{
fb.Option4__c= qw.question.Option_4__c;
}
if(qw.question.Option_5__c==qw.selectedRadio)
{
fb.Option5__c= qw.question.Option_5__c;
}
if(qw.question.Option_6__c==qw.selectedRadio)
{
fb.Option6__c= qw.question.Option_6__c;
}
if(qw.question.Option_7__c==qw.selectedRadio)
{
fb.Option7__c= qw.question.Option_7__c;
}
if(qw.question.Option_8__c==qw.selectedRadio)
{
fb.Option8__c= qw.question.Option_8__c;
}
if(qw.question.Option_9__c==qw.selectedRadio)
{
fb.Option9__c= qw.question.Option_9__c;
}
if(qw.question.Option_10__c==qw.selectedRadio)
{
fb.Option10__c= qw.question.Option_10__c;
}
feedbackList.add(fb);
}
if(qw.question.Question_Type__c=='Picklist')
{
Feedback__c fb = new Feedback__c();
fb.Question__c = qw.question.Id;
if(qw.question.Option_1__c==qw.selectedOption)
{
fb.Option1__c= qw.question.Option_1__c;
}
if(qw.question.Option_2__c==qw.selectedOption)
{
fb.Option2__c= qw.question.Option_2__c;
}
if(qw.question.Option_3__c==qw.selectedOption)
{
fb.Option3__c= qw.question.Option_3__c;
}
if(qw.question.Option_4__c==qw.selectedOption)
{
fb.Option4__c= qw.question.Option_4__c;
}
if(qw.question.Option_5__c==qw.selectedOption)
{
fb.Option5__c= qw.question.Option_5__c;
}
if(qw.question.Option_6__c==qw.selectedOption)
{
fb.Option6__c= qw.question.Option_6__c;
}
if(qw.question.Option_7__c==qw.selectedOption)
{
fb.Option7__c= qw.question.Option_7__c;
}
if(qw.question.Option_8__c==qw.selectedOption)
{
fb.Option8__c= qw.question.Option_8__c;
}
if(qw.question.Option_9__c==qw.selectedOption)
{
fb.Option9__c= qw.question.Option_9__c;
}
if(qw.question.Option_10__c==qw.selectedOption)
{
fb.Option10__c= qw.question.Option_10__c;
}
feedbackList.add(fb);
}
}
public void restrictMand()
{
if(qw.question.Is_Mandatory__c==true )
{
if(qw.question.Question_Type__c=='Descriptive'&& (qw.question.Answer__c==null||qw.question.Answer__c==''))
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,'Given question is mandatory, you cannot proceed'));
}
else if(qw.question.Question_Type__c=='Radio' && (qw.selectedRadio==null||qw.selectedRadio==''))
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,'Given question is mandatory, you cannot proceed'));
}
else if(qw.question.Question_Type__c=='Picklist' && (qw.selectedOption==null||qw.selectedOption==''))
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,'Given question is mandatory, you cannot proceed'));
}
else if(qw.question.Question_Type__c=='Checkbox')
{
if((qw.lstOptions.size()>=1 && qw.lstOptions[0].selectedanscheck==true)||(qw.lstOptions.size()>=2 && qw.lstOptions[1].selectedanscheck==true)||(qw.lstOptions.size()>=3 && qw.lstOptions[2].selectedanscheck==true)||(qw.lstOptions.size()>=4 && qw.lstOptions[3].selectedanscheck==true)||(qw.lstOptions.size()>=5 && qw.lstOptions[4].selectedanscheck==true)||
(qw.lstOptions.size()>=6 && qw.lstOptions[5].selectedanscheck==true)||(qw.lstOptions.size()>=7 && qw.lstOptions[6].selectedanscheck==true)||(qw.lstOptions.size()>=8 && qw.lstOptions[7].selectedanscheck==true)||(qw.lstOptions.size()>=9 && qw.lstOptions[8].selectedanscheck==true)||(qw.lstOptions.size()>=10 && qw.lstOptions[9].selectedanscheck==true))
{
index++;
qw = queWraList[index];
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,'Given question is mandatory, you cannot proceed'));
}
}
else
{
index++;
qw = queWraList[index];
}
}
else
{
index++;
qw = queWraList[index];
}
}
public class OptionWrapper
{
public String opt{get; set;}
public Integer Sr{get; set;}
public Boolean selectedanscheck{get;set;}
public Boolean selectedansRadio{get;set;}
public OptionWrapper(String str , Integer n)
{
selectedanscheck = false;
selectedansRadio = false;
this.opt=str;
this.Sr=n;
}
}
public class QuestionWrapper
{
public Question__c question {get;set;}
public integer srNo {get; set;}
public list<OptionWrapper> lstOptions{get;set;}
public boolean showMand {get; set;}
public List<SelectOption> radioValues{get;set;}
public List<SelectOption> picklistValues{get;set;}
public String selectedOption{get;set;}
public String selectedRadio{get;set;}
public QuestionWrapper (Question__c q)
{
this.question = q;
Integer i=1;
lstOptions = new list<OptionWrapper>();
while(i <= 10 && question.get('Option_'+i+'__c')!=null){
String optStr = (String)question.get('Option_'+i+'__c');
if(optStr.length() > 0){
lstOptions.add(new OptionWrapper( (String)question.get('Option_'+i+'__c'),i));
}
i++;
}
if(question.Question_Type__c=='Picklist' && lstOptions.size()>0)
{
picklistValues = new List<SelectOption>();
for(OptionWrapper ow:lstOptions)
{
picklistValues.add(new SelectOption(ow.opt,ow.opt));
}
}
if(question.Question_Type__c=='Radio' && lstOptions.size()>0)
{
radioValues= new List<SelectOption>();
for(OptionWrapper ow:lstOptions)
{
radioValues.add(new SelectOption(ow.opt,ow.opt));
}
}
}
}
}
qw = queWraList[index];
in restrictmand() method and wherever i m using this method like getNext() etc
Please try two option:
1. Run the test class with seealldata=true parameter.
2. Please check by system.asssert that you are having a question created. in test class just after following line.
Question__c que1 = TestDataGenerator.createQuestion('Who',false,'Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Test','Radio',sur1);
Thanks,
Himanshu