You need to sign in to do that
Don't have an account?
Farhad Keywan
Problem in code coverage
Hi,
I am developing a appexchange and most of the apex code is based on webservice. There is a apex generated class from wsdl also. So how can i increase my code coverage of test class and minimum 75% code coverage is required to publish app?
Thanks,
Soni
Hi
Could u post ur Class and Test class plz.
Regards,
Rajesh.
Hi SRS8,
Apex class code snippt (which is not covered in test Case) and test case code is given below -
Apex Class :
public with sharing class SearchEvents{
public String Message {get;set;}
public String RecordID {get;set;}
public String ContactID {get;set;}
public string SearchText {get;set;} //String for getting value of search Text box
public User currentUser {get;set;}
public string location {get;set;} // string for getting value from location select list`
public String State {get;set;}
public string loc {get;set;}
public String Country{get;set;}
public Integer pageNo {get;set;}
public Transient List<String> AddressList {get;set;}
public List<Event__c> SearchEventList {get;set;} //Map for storing all the events
public List<Event__c> EventList {get;set;}
public List<Selectoption> options {get;set;} // Select list for dispalying locations of events
public Integer Counter {get;set;}
public ID SearchName {get;set;}
public String venueName {get;set;}
public String OrgName {get;set;}
Public Event__c ServerRecord {get;set;}
public Integer Sdate {get;set;}
Public Integer Stime {get;set;}
public Integer Edate {get;set;}
Public Integer Etime {get;set;}
public List<String> LocationList {get;set;}
public List<Event__c> Events {get;set;}
public Advance_Search__c AdSearch {get;set;}
public Event__c e {get;set;}
public List<WrapperEvent> Outlooklist {get;set;}
public List<WrapperEvent> EvntList {get;set;}
public list<string> KeywordList {get;set;}
public ApexPages.StandardSetController setCon {get;set;}
//**********************************Wrapper class for event**********************************
public class WrapperEvent{
public Event__c evnt {get;set;}
public boolean selected {get;set;}
public String Dstart {get;set;}
public String DsTime {get;set;}
public string DEnd {get;set;}
Public String DEtime {get;set;}
public WrapperEvent(Event__c e){
selected=true;
evnt= new Event__c();
evnt=e;
}
}
//**********************************Method for CreateEvent button*****************************
//**************************Constructor of class**********************************
enterpriseSoapSforceCom.Soap sp ; //Initialization of soap variables
user us;
public SearchEvents(){
SearchEventList = new List<event__c>();
integer k= 1000;
ServerRecord = new Event__c();
EventList = new list<Event__c>();
keywordList = new List<String>();
EvntList = new List<WrapperEvent>();
LocationList= new List<String>();
transient Date dtConverted = Date.today(); // getting today date for searching only upcoming event
transient string dt = String.valueOf(dtConverted); // coverting date in to string
transient List<String> dtConvertedList = new List<string>();
dtConvertedList=(dt.split(' ')); //spliting date
SearchEventList = new List<Event__c>();
country = ApexPages.currentPage().getParameters().get('City'); // getting all cities within 75 miles
Set<String> DummyASet = new Set<String>();
if(country!=null){
AddressList=country.split(';',100); // adding cities in list
DummyASet.addAll(AddressList);
}
Cre__c cre = new Cre__c();
try{
cre =[ Select UserName__c,Paswd__c from Cre__c limit 1];
}catch(System.queryException e){}
sp = new enterpriseSoapSforceCom.Soap(); // Creating instance of class
String username = cre.UserName__c;
String password = cre.Paswd__c;
Transient Set<String> dummySet = new Set<String>(); // set for holding values of address for autocomplte list
Transient Set<String> dummyKeywordSet = new Set<String>(); // set for holding values for keyword
try{
enterpriseSoapSforceCom.LoginResult loginResult = sp.login(username, password); // login to server org
sp.endpoint_x=loginResult.Serverurl; // Setting server url to login url
enterpriseSoapSforceCom.SessionHeader_element sessionHeader = new enterpriseSoapSforceCom.SessionHeader_element();
sessionHeader.sessionId = loginResult.sessionId; // Setting session id
sp.SessionHeader=sessionHeader; //setting session header
enterpriseSoapSforceCom.QueryResult result=sp.query('Select id,Title__c,Description__c,agenda__c,Category_1__c,org_State__c, vAddress1__c,org_Zip__c,vcity__c,vCountry__c,vState__c,vZip__c ,category2__c,Category_3__c,Category_4__c,Day__c,Event_Start__c,Event_End__c,month__c,Organizer_Id__c,Time_modified__c,orgCity__c,orgCountry__c,Weekday__c,Year__c from Event__c where (Agenda__c!=null OR Organizer_id__c!=null) AND ( Event_Start__c >= ' + dtConvertedList[0]+' ) AND (Status__c= 0) Order BY Event_Start__c ')
boolean done = false;
string HTML_TAG_PATTERN = '<.*?>';
// compile the pattern
pattern myPattern = pattern.compile(HTML_TAG_PATTERN);
// loop which iterate over all retunred batch size of server
while (! done) {
List<sobjectEnterpriseSoapSforceCom.sObject_x> objList= result.Records; // getting first batch size of record
//system.debug('size is'+objList.size());
for(sobjectEnterpriseSoapSforceCom.sObject_x obj:objList){
if(((DummyASet.contains(obj.vcity)) || (DummyASet.contains(obj.oCity)))){
Event__c evnt = new Event__c(); // creating virtual event at client side
// Initializing variables..
evnt.name=obj.name;
evnt.title__c=obj.zone;
evnt.Agenda__c=obj.vName;
evnt.Category_1__c=obj.cat1;
evnt.category2__c=obj.cat2;
evnt.Category_3__c=obj.cat3;
evnt.Category_4__c=obj.cat4;
evnt.Contact__c=obj.ConID;
//evnt.vAddress1__c=obj.vAddress1;
//evnt.vCity__c=obj.vCity;
//evnt.vState__c=obj.vState;
//evnt.vZip__c=obj.vZip;
evnt.Day__c=obj.day;
if(obj.Description!=null && obj.Description!='<p>' && obj.Description.length()>200){ //getting some part of description
evnt.Description__c=obj.Description.substring(0,200).trim();
// get your matcher instance
matcher myMatcher = myPattern.matcher(evnt.Description__c);
//remove the tags
evnt.Description__c = myMatcher.replaceAll('');
//evnt.Description__c = evnt.Description__c.replaceAll('<tr>','');
//system.debug('des is'+evnt.Description__c);
}
else if(obj.Description!=null && obj.Description!='<p>'){
evnt.Description__c=obj.Description.trim();
// get your matcher instance
matcher myMatcher = myPattern.matcher(evnt.Description__c);
//remove the tags
evnt.Description__c = myMatcher.replaceAll('');
//evnt.Description__c = evnt.Description__c.replaceAll('<tr>','');
//system.debug('des is'+evnt.Description__c);
}
evnt.Event_Start__c=obj.STime;
evnt.Event_End__c=obj.ETime;
evnt.month__c=obj.mon;
evnt.Organizer_Id__c=obj.oid;
evnt.vAddress1__c=obj.vAddress1;
evnt.vCity__c=obj.vCity;
evnt.vZip__c=obj.vzip;
evnt.vState__c=obj.vState;
evnt.Time_modified__c=obj.timem;
evnt.Weekday__c=obj.week;
evnt.Year__c=obj.year;
evnt.ServerRecordID__c =obj.ID;
//if(((DummyASet.contains(obj.vcity)) || (DummyASet.contains(obj.oCity)))) // If event criteria match to local criteria
//EvntList.add(new WrapperEvent(evnt));
searchEventList.add(evnt);
EventList.add(evnt); // Adding event in my local list
}
// Adding Keywords in set for autocomplete list
if(obj.zone!=null && dummyKeywordSet.size()<=1000){
List<String> dummyList =obj.zone.tolowercase().split(' ',obj.zone.length());
if(dummyList.size()<(k-dummyKeywordSet.size()))
dummyKeywordSet.addAll(dummyList);
}
// Adding Venue city in set for autocomplete list
if(obj.vCity!=null && dummySet.size()<1000){
if(obj.vstate!=null )
dummySet.add(obj.vCity+', '+obj.vstate+' ');
else
dummySet.add(obj.vCity);
}
//Adding Organizer city in set for autocomplete list
if(obj.oCity!=null && dummySet.size()<1000){
if(obj.oState!=null)
dummySet.add(obj.oCity+', '+obj.oState+' ');
else
dummySet.add(obj.oCity);
}
//Adding Venue state in set for autocomplete list
if(obj.vState!=null && dummySet.size()<1000){
dummySet.add(obj.vState+' ');
}
if(obj.oState!=null && dummySet.size()<1000){ // Adding organizer state in set
dummySet.add(obj.oState+' ');
}
/*if(obj.vCountry!=null && dummySet.size()<1000){ // adding venue country in set
dummySet.add(obj.vCountry);
}
if(obj.oCountry!=null && dummySet.size()<1000){ // adding Org country in set
dummySet.add(obj.oCountry);
}*/
if(obj.vzip!=null && dummySet.size()<1000){ // Adding venue zip in set
dummySet.add(obj.vzip);
}
if(obj.oZip!=null && dummySet.size()<1000){ // adding org zip code
dummySet.add(obj.oZip);
}
}
if(result.done){ // if no more result batch
done = true;
}
else{
result=sp.queryMore(result.queryLocator); // getting another batch of record
}
}
record();
if(dummyKeywordset.size()>0)
keywordList.addAll(dummyKeywordset); // Add keyword set in list
if(dummySet.size()>0)
locationList.addAll(dummySet); // add location set in list
}catch(Exception e){
}
AdvanceSearch();
dummySet=null;
dummyKeywordset=null;
}
}
Thanks,
Soni
Test Case :
Test Class:-
@isTest
private class TestSearchEvents{
static testMethod void TestSearch(){
/* Contact con = new Contact();
con.firstname='soni';
con.lastName='rajput';
con.Phone='6551561';
con.email='demo@gmail.com';
insert con; */
Event__c evnt = new Event__c();
evnt.name='test';
//evnt.agenda__c = 'testing';
evnt.vAddress1__c = 'streetg';
evnt.vAddress2__c= 'streetp';
evnt.vAddress3__c='streetn';
evnt.vCity__c='Los';
evnt.vzip__c='9956';
evnt.vCountry__c='USA';
evnt.organizer_id__c='org1 chamber';
evnt.org_Phone__c='865455';
evnt.orgAddress1__c='street1';
evnt.orgAddress2__c='street2';
evnt.orgADdress3__c='street3';
evnt.orguri__c='www.salesforce.com';
evnt.orgCity__c='San francisco';
//evnt.Contact__c=con.id;
evnt.org_State__c='CA';
evnt.org_zip__c='15528';
evnt.title__c='test';
evnt.agenda__c='venue1 chamber';
evnt.description__c='chamber';
evnt.event_start__c=date.today();
evnt.event_end__c=date.today()+1;
insert evnt;
evnt.ServerRecordID__c=evnt.id;
upsert evnt;
Event__c evnt2 = new Event__c();
evnt2.name='test';
evnt2.organizer_id__c='org1';
evnt2.org_Phone__c='865455';
evnt2.orgAddress1__c='street1';
evnt2.orgAddress2__c='street2';
evnt2.orgADdress3__c='street3';
evnt2.orguri__c='www.salesforce.com';
evnt2.orgCity__c='San francisco';
evnt2.org_State__c='CA';
evnt2.org_zip__c='15528';
evnt2.title__c='test chamber';
evnt2.agenda__c='venue1';
evnt2.description__c='chamber';
evnt2.category_1__c='chamber';
evnt2.event_start__c= date.newinstance(2012,12, 7);
evnt2.event_end__c=evnt2.event_start__c-1;
insert evnt2;
Event__c evnt1 = new Event__c();
evnt1.name='test';
evnt1.organizer_id__c='org1';
evnt1.org_Phone__c='865455';
evnt1.orgAddress1__c='street1';
evnt1.orgAddress2__c='street2';
evnt1.orgADdress3__c='street3';
evnt1.orguri__c='www.salesforce.com';
evnt1.orgCity__c='San francisco';
evnt1.org_State__c='CA';
evnt1.org_zip__c='15528';
evnt1.title__c='test';
evnt1.agenda__c='venue1';
evnt2.category2__c='chamber';
evnt2.category_3__c='chamber';
evnt2.category_4__c='chamber';
evnt1.description__c='chamber';
insert evnt1;
Advance_Search__c ad = new Advance_Search__c();
ad.name ='new';
ad.Keyword__c='chamber';
ad.Location__c='CA';
insert ad;
//ApexPages.StandardController stdController1 = new ApexPages.StandardController(p1);
SearchEvents se=new SearchEvents ();
test.startTest();
se.RecentSearch();
se.searcheventList.add(evnt);
se.eventList.add(evnt);
se.eventList.add(evnt1);
se.eventList.add(evnt2);
se.Location='All US';
se.searchText='chamber';
se.demo();
se.Location='Matinsville, NJ';
se.demo();
se.Location='Martinsville';
se.demo();
se.Location='My Local';
se.demo();
se.record();
se.EvntList[0].selected=true;
se.EvntList[1].selected=true;
se.EvntList[2].selected=true;
//se.serverRecord=se.EvntList[0].evnt;
se.recordid = evnt.id;
se.Addtosalesforce();
se.MassSalesforce();
se.MassOutlook();
se.recordId=evnt.id;
se.Actionmethod();
se.VenueMethod();
se.OrgMethod();
se.loc='CA';
se.AdvanceSearch();
se.searchname=ad.id;
se.adsearch.name='new';
se.SaveAdvanceSearch();
se.saveSearch();
se.changeObject();
se.Advancesearchresult();
se.DeleteAdvanceSearch();
se.clear();
se.check();
se.venuedetail();
se.orgdetail();
se.serverRecord=evnt;
//se.ContactId=con.id;
se.AddContact();
se.AddToOutlook();
se.cancel();
se.samescreen();
se.sdate=12052012;
se.edate=12072012;
se.stime=121315;
se.etime=121515;
se.SaveEventRelatedRecord();
se.etime=21515;
se.SaveEventRelatedRecord();
se.stime=21315;
se.etime=21515;
se.SaveEventRelatedRecord();
se.stime=21315;
se.etime=121515;
se.SaveEventRelatedRecord();
se.sdate=null;
se.edate=null;
se.stime=null;
se.etime=null;
se.SaveEventRelatedRecord();
se.RecordId='';
se.AddtoSalesforce();
se.EvntList[0].selected=false;
se.EvntList[1].selected=false;
se.EvntList[2].selected=false;
se.searchName='01pd0000001Q4iU';
se.changeObject();
se.advancesearch();
//se.adsearch=null;
se.saveAdvancesearch();
system.AssertEquals(Evnt.event_start__c,Date.Today());
test.stopTest();
}
}
Hi
Here u want to add some code to the test class and one more thing is u added the obj what is this i can't understand.
And also u call the methods more than one time in test class with an object.Try to call one time.
SearchEvents.Country=ApexPages.currentPage().getParameters().put('City', 'USA');
Cre__c cr = new Cre__c();
cr.UserName__c='naveen@busapp.com';
cr.Paswd__c='testing123AsOe6vH1LFP00ec2AiaWXhW6J';
insert cr;
SearchEvents.WrapperEvent sw = new SearchEvents.WrapperEvent(evnt);
Regards,
Rajesh.
Hi SRS8,
I have added this part still code coverage is only 57%.
Thanks,
Soni
Hi
Can u color the lines with red which are not covered in ur class.and I asked about the obj inur class what is it.
Regards,
Rajesh.
Hi Rajesh,
Following code is not covered in test case.
enterpriseSoapSforceCom.LoginResult loginResult = sp.login(username, password); // login to server org
sp.endpoint_x=loginResult.Serverurl; // Setting server url to login url
enterpriseSoapSforceCom.SessionHeader_element sessionHeader = new enterpriseSoapSforceCom.SessionHeader_element();
sessionHeader.sessionId = loginResult.sessionId; // Setting session id
sp.SessionHeader=sessionHeader; //setting session header
enterpriseSoapSforceCom.QueryResult result=sp.query('Select id,Title__c,Description__c,agenda__c,Category_1__c,org_State__c, vAddress1__c,org_Zip__c,vcity__c,vCountry__c,vState__c,vZip__c ,category2__c,Category_3__c,Category_4__c,Day__c,Event_Start__c,Event_End__c,month__c,Organizer_Id__c,Time_modified__c,orgCity__c,orgCountry__c,Weekday__c,Year__c from Event__c where (Agenda__c!=null OR Organizer_id__c!=null) AND ( Event_Start__c >= ' + dtConvertedList[0]+' ) AND (Status__c= 0) Order BY Event_Start__c ')
boolean done = false;
string HTML_TAG_PATTERN = '<.*?>';
// compile the pattern
pattern myPattern = pattern.compile(HTML_TAG_PATTERN);
// loop which iterate over all retunred batch size of server
while (! done) {
List<sobjectEnterpriseSoapSforceCom.sObject_x> objList= result.Records; // getting first batch size of record
//system.debug('size is'+objList.size());
for(sobjectEnterpriseSoapSforceCom.sObject_x obj:objList){
if(((DummyASet.contains(obj.vcity)) || (DummyASet.contains(obj.oCity)))){
Event__c evnt = new Event__c(); // creating virtual event at client side
// Initializing variables..
evnt.name=obj.name;
evnt.title__c=obj.zone;
evnt.Agenda__c=obj.vName;
evnt.Category_1__c=obj.cat1;
evnt.category2__c=obj.cat2;
evnt.Category_3__c=obj.cat3;
evnt.Category_4__c=obj.cat4;
evnt.Contact__c=obj.ConID;
//evnt.vAddress1__c=obj.vAddress1;
//evnt.vCity__c=obj.vCity;
//evnt.vState__c=obj.vState;
//evnt.vZip__c=obj.vZip;
evnt.Day__c=obj.day;
if(obj.Description!=null && obj.Description!='<p>' && obj.Description.length()>200){ //getting some part of description
evnt.Description__c=obj.Description.substring(0,200).trim();
// get your matcher instance
matcher myMatcher = myPattern.matcher(evnt.Description__c);
//remove the tags
evnt.Description__c = myMatcher.replaceAll('');
//evnt.Description__c = evnt.Description__c.replaceAll('<tr>','');
//system.debug('des is'+evnt.Description__c);
}
else if(obj.Description!=null && obj.Description!='<p>'){
evnt.Description__c=obj.Description.trim();
// get your matcher instance
matcher myMatcher = myPattern.matcher(evnt.Description__c);
//remove the tags
evnt.Description__c = myMatcher.replaceAll('');
//evnt.Description__c = evnt.Description__c.replaceAll('<tr>','');
//system.debug('des is'+evnt.Description__c);
}
evnt.Event_Start__c=obj.STime;
evnt.Event_End__c=obj.ETime;
evnt.month__c=obj.mon;
evnt.Organizer_Id__c=obj.oid;
evnt.vAddress1__c=obj.vAddress1;
evnt.vCity__c=obj.vCity;
evnt.vZip__c=obj.vzip;
evnt.vState__c=obj.vState;
evnt.Time_modified__c=obj.timem;
evnt.Weekday__c=obj.week;
evnt.Year__c=obj.year;
evnt.ServerRecordID__c =obj.ID;
//if(((DummyASet.contains(obj.vcity)) || (DummyASet.contains(obj.oCity)))) // If event criteria match to local criteria
//EvntList.add(new WrapperEvent(evnt));
searchEventList.add(evnt);
EventList.add(evnt); // Adding event in my local list
}
// Adding Keywords in set for autocomplete list
if(obj.zone!=null && dummyKeywordSet.size()<=1000){
List<String> dummyList =obj.zone.tolowercase().split(' ',obj.zone.length());
if(dummyList.size()<(k-dummyKeywordSet.size()))
dummyKeywordSet.addAll(dummyList);
}
// Adding Venue city in set for autocomplete list
if(obj.vCity!=null && dummySet.size()<1000){
if(obj.vstate!=null )
dummySet.add(obj.vCity+', '+obj.vstate+' ');
else
dummySet.add(obj.vCity);
}
//Adding Organizer city in set for autocomplete list
if(obj.oCity!=null && dummySet.size()<1000){
if(obj.oState!=null)
dummySet.add(obj.oCity+', '+obj.oState+' ');
else
dummySet.add(obj.oCity);
}
//Adding Venue state in set for autocomplete list
if(obj.vState!=null && dummySet.size()<1000){
dummySet.add(obj.vState+' ');
}
if(obj.oState!=null && dummySet.size()<1000){ // Adding organizer state in set
dummySet.add(obj.oState+' ');
}
/*if(obj.vCountry!=null && dummySet.size()<1000){ // adding venue country in set
dummySet.add(obj.vCountry);
}
if(obj.oCountry!=null && dummySet.size()<1000){ // adding Org country in set
dummySet.add(obj.oCountry);
}*/
if(obj.vzip!=null && dummySet.size()<1000){ // Adding venue zip in set
dummySet.add(obj.vzip);
}
if(obj.oZip!=null && dummySet.size()<1000){ // adding org zip code
dummySet.add(obj.oZip);
}
}
if(result.done){ // if no more result batch
done = true;
}
else{
result=sp.queryMore(result.queryLocator); // getting another batch of record
}
}
Thanks,
Soni
Hi
As I see there are some classes are used in this for eg: enterpriseSoapSforceCom.Soap try o create the object for it i.e
enterpriseSoapSforceCom.Soap ess = new enterpriseSoapSforceCom.Soap();
like this for others also.
Regards,
Rajesh.
Hi Rajesh,
enterpriseSoapSforceCom.Soap is a apex class generated through wsdl and error is coming when i am using login method for logging on server.
Thanks,
Soni
Hi
U keep this statement enterpriseSoapSforceCom.Soap ess = new enterpriseSoapSforceCom.Soap();
it saving?
Regards,
Rajesh.
Hi
Did u get the code coverage.
Regards,
Rajesh.
Hi Rajesh,
Yeah it saving but the code coverage of that class is 0%.
Thanks,
Soni
Hi
Yeah it saving but the code coverage of that class is 0%.
Thanks,
Soni
Hi GM
here what ever a classes used in this just create instance for each class eg:
enterpriseSoapSforceCom ess = new enterpriseSoapSforceCom();
enterpriseSoapSforceCom.Soap esss = new enterpriseSoapSforceCom.Soap();
And for this class don't run this class indivisually,Instead run all the test this option will find setup->develop->Apex Classes->Run All test.
Regards,
Rajesh.