You need to sign in to do that
Don't have an account?
Code Coverage
Hi, i am writing code coverage for my org and facing problem while writing code coverage for class below.
the problem is due to 'RisksConversationId' that i cann't get. Please give me some suggestion as i can made at least 80% code coverage for this class. I am new to code coverage. please help me. Thanks in advance.
global with sharing class RisksConversationGEO
{
Id RisksConversationId;
public string rcName{get; set;}
public string lat{get; set;}
public string lon{get; set;}
string rcLatLon;
List<Risk_Conversation__c> rConvDetails= new List<Risk_Conversation__c>();
public RisksConversationGEO(ApexPages.StandardController controller)
{
system.debug('RisksConversationId:'+ ApexPages.currentPage().getParameters().get('id'));
RisksConversationId = ApexPages.currentPage().getParameters().get('id');
getlocreg();
/* rConvDetails = [select Twitter_GEO__c, Name from Risk_Conversation__c where Id =: RisksConversationId];
rcName = rConvDetails[0].Name;
if(rConvDetails[0].Twitter_GEO__c != null)
{
rcLatLon = rConvDetails[0].Twitter_GEO__c;
string[] rcLatLonArr = rcLatLon.split(' ');
lat = rcLatLonArr[0];
lon = rcLatLonArr[1];
system.debug('lat :'+lat +'lon :'+lon);
} */
// system.debug('risk1_Id: '+ RiskId); ApexPages.StandardController controller
}
public RisksConversationGEO(Id risksConversation1_Id)
{
if( RisksConversationId == null)
{
RisksConversationId = risksConversation1_Id;
system.debug('risksConversation1_Id: '+ RisksConversationId);
//rcName = [select Name from Risk_Conversation__c where Id =:RisksConversationId ];
//riskName = rcName[0].Name;
getlocreg();
}
}
public void getlocreg()
{
rConvDetails = [select Twitter_GEO__c, Name from Risk_Conversation__c where Id =: RisksConversationId];
rcName = rConvDetails[0].Name;
if(rConvDetails[0].Twitter_GEO__c != null)
{
rcLatLon = rConvDetails[0].Twitter_GEO__c;
string[] rcLatLonArr = rcLatLon.split(' ');
lat = rcLatLonArr[0];
lon = rcLatLonArr[1];
system.debug('lat :'+lat +'lon :'+lon);
}
}
}
Id RisksConversationId;
Just make this variable public or add @testVisible annotaion to this variable.
Then insert a record of RisksConversation in test class .
Then the class variable which you have created (dot).RisksConversationId=classrefVariable.RisksConversation record which you have inserted that (dot).Id.
Hello Situ
, thanks for your quick reply.
can you explain how to assign classrefvariable as i am not getting this.
and the test method still gives error
"System.ListException: List index out of bounds: 0"
please find my test method here
Test method.:
@isTest(seeAllData=true)
public class testMtd_RisksConversationGEO{
static testMethod void rskconvgeo(){
Risk_Conversation__c rskconv = new Risk_Conversation__c();
RisksConversationGEO obj = new RisksConversationGEO(new ApexPages.StandardController(rskconv));
rskconv.Twitter_GEO__c = 'Risk';
insert rskconv;
//LIST<water_demo__Risk_Conversation__c> Idl = [select Id from Risk_Conversation__c where Twitter_GEO__c = 'Risk'];
//Id RisksConversationId = Idl[0].Id; // i am getting problem here.....
// another way i tried...but not working
// Instantiate VisualForce Page
PageReference pgRef = Page.RiskConversationMap; //Create Page Reference
Test.setCurrentPage(pgRef); //Set the page for Test Method
ApexPages.currentPage().getParameters().put('id', rskconv.id);//Pass Id to page
obj.getlocreg();
}
}
Thanks again
@isTest(seeAllData=true)
public class testMtd_RisksConversationGEO{
static testMethod void rskconvgeo(){
Risk_Conversation__c rskconv = new Risk_Conversation__c();
rskconv.Twitter_GEO__c = 'Risk';
insert rskconv;
RisksConversationGEO obj = new RisksConversationGEO(new ApexPages.StandardController(rskconv));
obj.RisksConversationId=rskconv.Id;
obj.getlocreg();
}
}
Check this but one thing you need to check Risk_Conversation__c record should insert for that you need to give all mandatory fileds.
Thanks Situ..
But i am still not able to pass test method..same error "List index out of bounds". I made all the changes as you suggested and made all the required fields availabel for test data. still stucks.but thanks for your detailed explanations.