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
gautam123gautam123 

test method for the class

Hi all,
I have written two classes and a vf page to add multiple contacts in a custom object .But now the issue is i am not able to achieve 75% code coverage for one of my classes .
Can anyone of you help me to get this 75% code coverage for the class !
Please find the classes attached below .The issue is with the test class "TestEventMassAddControllerExt".


EventMassAddControllerExt :
// This controller is used to mass signup contacts for a event Activity.
public class EventMassAddControllerExt
{   
private SFDC_Special_Event__c se;
public String name {get;set;}
public String id {get;set;}
public Integer TotalRegistrants {get;set;}
public Integer size {get;set;}
public Integer noOfRecordsToBeDisplayed {get;set;}
public static Integer maxRecordsPerPage = 25;
public Integer index{get;set;}
public String fname {get;set;}
public String lname {get;set;}
public EventMassAddControllerExt(ApexPages.StandardController stdController)
{
this.se = (SFDC_Special_Event__c) stdController.getRecord();
init();       
System.debug(Logginglevel.DEBUG, 'Constructor ' + se.Id);
}  
private Integer countQuery()
{
System.debug(Logginglevel.DEBUG, 'Executing Count Query ');
if (fname != null && fname != '' && lname != null && lname != '' )
{
return [select count() from Contact where FirstName like: fname+'%' and LastName like: lname+'%' ];
}
else if (fname != null && fname != '' )
{
return [select count() from Contact where FirstName like: fname+'%' ];
}
else if (lname != null && lname != '' )
{
return [select count() from Contact where LastName like: lname+'%' ];
}
else
{
System.debug(Logginglevel.DEBUG, 'Returning Count');           
return [select count() from Contact where Id!=null limit:500 ];
}
}
private List<Contact> resultsQuery(Integer limitRows, Integer next)
{
System.debug(Logginglevel.DEBUG, 'Executing Results Query ' + limitRows + 'next ' + next);
String query = ' select Id,name,FirstName,LastName,Email,Phone '+ ' from Contact '+' where Id!=null ';
String orderQuery = ' order by name '+ ' limit ' + limitRows ;                            
Integer rowCount = 0;
List<Contact> retContactList = new List<Contact>();
if (fname != null && fname != '' && lname != null && lname != '' )
{
for (List<Contact> contacts : [select Id,Name,FirstName,LastName,Email,Phone from Contact
where FirstName like : fname+'%' and LastName like : lname+'%' order by LastName limit:500])
{
for (Contact con : contacts)
{
if (rowCount >= next)
{
retContactList.add(con);
}
rowCount++;
}
}
}
else if (fname != null && fname != '' )
{
for (List<Contact> contacts : [select Id,name,FirstName,LastName,Email,Phone
from Contact where FirstName like : fname+'%' order by LastName limit:500])
{
for (Contact con : contacts)
{
if (rowCount >= next)
{
retContactList.add(con);
}
rowCount++;
}
}
}
else if (lname != null && lname != '' )
{
for (List<Contact> contacts : [select Id,name,FirstName,LastName,Email,Phone
from Contact where LastName like : lname+'%'order by LastName limit:500])
{
for (Contact con : contacts)
{
if (rowCount >= next)
{
retContactList.add(con);
}
rowCount++;
}
}
}
else
{
for (List<Contact> contacts : [select Id,name,FirstName,LastName,Email,Phone
from Contact where FirstName!=null and LastName!=null order by LastName limit:500 ])
{
for (Contact con : contacts)
{
if (rowCount >= next)
{
retContactList.add(con);
}
rowCount++;
}
}
}
System.debug(Logginglevel.DEBUG, 'Returning Results Query ' + retContactList.size());
return retContactList;
}
public Integer getCount()
{
return countQuery();
}
public List<Contact> getContactList(String id, Integer next)
{   
Integer rowCount = 0;      
Integer totRows = next + maxRecordsPerPage;
return resultsQuery(totRows, next);
}
public PageReference massSignUp()
{
String vaId = se.Id;
ApexPages.Message res;
if (selUsers.size() > 0)
{
res = eventmain.signUpBatch(vaId, selUsers);           
id = vaId;
Double totalParticipants = se.Total_Registrants__c;
TotalRegistrants = totalParticipants.intValue();           
}
else
{
res = new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.SignupMassSelectAtLeastOneUser);
}
if (res != null)
{
ApexPages.addMessage(res);
return null;      
}
PageReference pageRef = Page.EventMassAddPage;
pageRef.getParameters().put('id', vaId);       
pageRef.setredirect(true);
return pageRef;
}
String[] selUsers = new String[]{};
public List<SelectOption> getItems() {     
System.debug(Logginglevel.DEBUG, 'Calling getItems');
List<SelectOption> options = new List<SelectOption>();
if (index == null)
{
index = 0;
}
List<Contact> allContacts = getContactList(se.Id, index);
System.debug(Logginglevel.DEBUG, 'All Contacts ' + allContacts.size());
Set<String> signedUpContacts = eventmain.getSignedUpUsers(se.Id, allContacts);
System.debug(Logginglevel.DEBUG, 'Signedup Contacts ' + signedUpContacts.size());
Integer lssize = allContacts.size(); 
if (size > index+maxRecordsPerPage) {
noOfRecordsToBeDisplayed = index+maxRecordsPerPage;
} else {
noOfRecordsToBeDisplayed = lssize;
}
Integer lSize = (noOfRecordsToBeDisplayed < lssize?noOfRecordsToBeDisplayed:lssize);
for (Integer i=0;i<lSize;i++) {
Contact u = allContacts[i];  
Boolean signedUp = false;
if (signedUpContacts.contains(u.Id)) {
signedUp = true;
}
String displayValue = 'Name:'+' '+u.Name;
if (u.Email!= null) {
displayValue += ' |   ' +'Email:'+' '+ u.Email;
if(u.Phone!=null) {
displayValue += ' |   ' +'Phone:'+' '+ u.Phone;
}
}
if (signedUp) {
options.add(new SelectOption(u.Id,displayValue,true));
} else {
options.add(new SelectOption(u.Id,displayValue));
}
}
System.debug(Logginglevel.DEBUG, 'Returning getItems ' + options.size());
return options;
}
public String[] getselUsers() {
return selUsers;
}
public void setSelUsers(String[] selUsers) {
this.selUsers = selUsers;
}
public void init()
{
System.debug(Logginglevel.DEBUG, 'Calling init()');
System.debug(Logginglevel.DEBUG, 'init volact name ' + se.Name);
id = se.Id;
name = se.Name;       
Double totalParticipants = se.Total_Registrants__c;
if (totalParticipants != null) {
TotalRegistrants = totalParticipants.intValue();
}
size = getCount();
index = 0;
System.debug(Logginglevel.DEBUG, 'Completing init()');
}
public void search() {
size = getCount();
index = 0;
}
public void nextPage() {
if (index == null) index = 0;
index = (index+maxRecordsPerPage);
if (index > size) {
index = size;
}
}
public void prevPage() {
index = (index-maxRecordsPerPage);
if (index < 0) {
index = 0;
}
}
public Boolean getRenderNext() {
if (index == null) {
index = 0;
 }
if (index+maxRecordsPerPage >= size) {
return false;
}
return true;
}
public Boolean getRenderPrev() {       
if (index > 0 ) {
return true;
}
return false;
}
public Boolean getRenderMaxParticipants() {
if (TotalRegistrants != null && TotalRegistrants > 0) {
return true;
}
return false;
}
}
 
Test EventMassAddControllerExt :
Problem lies in this class ..achieved just 62% code coverage .not able to achieve 75%+..plz help!!
 
@isTest
private class TestEventMassAddControllerExt
{   
private EventMassAddControllerExt setupController(SFDC_Special_Event__c va)
{
// Construct the controller that will be returned by this setup method.
ApexPages.StandardController standardcontroller = new ApexPages.StandardController(va) ;
EventMassAddControllerExt controller = new EventMassAddControllerExt(standardcontroller);
// Create a page for use in the test.
PageReference p = Page.EventMassAddPage;
// Set the case ID in the context for use by the controller.
p.getParameters().put('id',va.id);
// Set the case ID in the context for use by the controller.
Test.setCurrentPage(p);
return controller;
}
private SFDC_Special_Event__c setupTestCaseVA()
{
datetime myDateTime = datetime.now();
SFDC_Special_Event__c va1 = new SFDC_Special_Event__c(name='testevent',Type__c='Awareness',
Start_Date__c=myDateTime,ME_Status__c='Planned',Duration__c='1');
insert(va1);
Contact onecon=new Contact(lastname='testcon');
insert onecon;
System.assertEquals('testcon', onecon.lastname);
List<Contact> contacts = new List<Contact>();
for(integer i=0; i<20; i++) {
contacts.add( new Contact(lastname = 'a+i'));
}
insert contacts;
List<SFDC_Attendence__c> attendeeList = new List<SFDC_Attendence__c>();
for (Contact contact : contacts)
{
SFDC_Attendence__c attendees = new SFDC_Attendence__c(Special_Event__c=va1.Id,Attendee__c=contact.Id);
attendeeList.add(attendees);
}
return va1;
}
public static testMethod void massSignUpTest()
{
TestEventMassAddControllerExt testclass = new TestEventMassAddControllerExt();
SFDC_Special_Event__c va = testclass.setupTestCaseVA();
EventMassAddControllerExt controller = testclass.setupController(va);
controller.nextPage();
controller.prevPage();
controller.getRenderNext();
controller.getRenderPrev();
System.debug(Logginglevel.DEBUG, 'Mass sign up res: '+controller.massSignUp());
String[] selectedUsers = new String[1];
controller.setSelUsers(selectedUsers);
System.assertEquals(controller.getSelUsers().size(),1);
controller.fname='m';   
controller.search();
controller.getItems();
controller.getselUsers();
controller.getRenderNext();
controller.getRenderPrev();
controller.lname='a1';
controller.search();
controller.getItems();
controller.getselUsers();
controller.getRenderNext();
controller.getRenderPrev();
controller.fname='g';
controller.lname='r';
controller.search();
controller.getCount();
controller.getItems();
controller.getselUsers();
controller.init();
controller.nextPage();
controller.prevPage();
controller.getRenderNext();
controller.getRenderPrev();
controller.getRenderMaxParticipants();
Test.startTest();
Test.stopTest();    
}
}

gm_sfdc_powerdegm_sfdc_powerde
What exactly is your problem that's preventing you from achieving 75% code coverage? If you run your tests from IDE, you can clearly see from output log the lines that are not covered.  Ditto with running tests from the app. 
gautam123gautam123

Hi,

 

I am new to writing apex classes .I looked at many different apex codes and with help from all these codes ,developed the class "EventMassAddControllerExt" .But for the test class i am not able to achieve 75%+ code coverage .I don't exactly know what i am missing in the test class .Right now i have just achieved 62% code coverage .

 

So can you please help me develope the test class so that i can cross 75% code coverage .Thanks in advance .

gm_sfdc_powerdegm_sfdc_powerde
Are you running the tests from the app UI? If yes, click on the code coverage % and it would show you the lines of code that are not covered by your tests.  Now go back and design your tests in such a way that uncovered lines will get executed.  If you are running your tests from IDE, you can see the uncovered lines in the output section.  Hope this helps!
gautam123gautam123

yes i am running the test method in app UI .I could also see the lines not covered in the main class .

I am trying to get these lines covered in my test method since last three days,but in vain .

 

Can you please look at the test method and suggest me how to proceed so that i get atleast 75% code coverage .

A little help will be appreciated ...Thanks

ccMarkccMark

Without actually pulling this into one of my development orgs and building it out, I can't see specifically what your problem is.  However, I noticed that you've got a lot of "IF" statements in your code in various places.

 

In developing my own test methods, I've found this to be a problem because it's often difficult to create a data set that covers every concievable test case.

 

I want to echo the comments made by the other's who've answered this post and say to look at the code coverage % linked page and see specifically which lines aren't covered.  That should tell you how what's not getting processed using your test cases.  More than likely what you'll discover is that you need some additional test data and some new test cases.