+ Start a Discussion
DoctorDoctor 

{get; set} test method question

I am writing a test method for the controller below and I am stuck at 54%. It seems that where I am stuck is when trying simulate selecting Contacts from the search results prior to saving. I thought that 'controller.allContactselected = true;' would select all results and allow a save to occur, but this doesn't seem to be the case.
 
Any help would be greatly appreciated.
 
Controller:

public with sharing class contactSearchDBGExtension { 

 

public Group_Call__c GroupCall {get; private set;}

public boolean SearchDisplay {get; set;}

public boolean SelectDisplay {get; set;} 

 

public contactSearchDBGExtension(ApexPages.StandardSetController controller) {}  

 

public contactSearchDBGExtension(ApexPages.StandardController controller) {

GroupCall = new Group_Call__c();SearchDisplay = false;SelectDisplay = false;

}  

 

public PageReference save() {try {upsert(GroupCall);

} catch(System.DMLException e) {ApexPages.addMessages(e);return null;}

SearchDisplay = true;

return null;}  

 

public boolean allContactselected {get; set;}

Map<String, boolean> SelectedContact = new Map<String, boolean>{};

 

//checkbox per page

public boolean allCheckboxes = false;

public Integer getSelectedContact(){return SelectedContact.size();}

 

 /* Get Search Criteria Values */

//public String Name {get; set;}

public String getName()

{return Name;} 

 

public String getState()

{return State;}  

 

public String getSpecialty()

{return Specialty;} 

 

/* Set Results List */

public List<Contact> result = new List<Contact>();

List<contactwrapper> resultConList = new List<contactwrapper>();

List<contact> selectedContacts = new List<contact>();

List<Attendee__c> AttendeeList = new List<Attendee__c>();

List<Event> EventList = new List<Event>(); 

 

private String Name;

private String State;

private String Specialty;

private integer countfig; 

 

public contactSearchDBGExtension(){}  

public List<contact> getResult() {return result;}  

public List<contactwrapper> getConRes()

{resultConList.clear();countfig = 0;

 

if (result.size() > 0){

for(Contact c : result){countfig = countfig +1;if (countfig < 1000) resultConList.add(new contactwrapper(c));}}

return resultConList; 

//return result;} 

 

public PageReference getSelected() {

selectedContacts.clear();for(contactwrapper conwrapper : resultConList)

if(conwrapper.selected == true)

selectedContacts.add(conwrapper.con);

return null;} 

 

public List<contact> GetSelectedContacts() { 

if(selectedcontacts.size()>0)return selectedcontacts; 

else

return null;} 

 

public PageReference SaveSelectedContacts() {

if (selectedcontacts.size() > 0){

for(Contact c : selectedcontacts){

Attendee__c AttIns = new Attendee__c();

Event EventIns = new Event();

AttIns.Attendee__c = c.Id;

AttIns.Group_Call__c = GroupCall.Id;

AttendeeList.add(AttIns);

EventIns.whatID = GroupCall.Id;

EventIns.whoID = c.Id;

EventIns.StartDateTime = GroupCall.CallStart__c; 

 

if(GroupCall.Session_Length__c == '15 mins'){

EventIns.DurationInMinutes = 15;}

else if(GroupCall.Session_Length__c == '30 mins'){

EventIns.DurationInMinutes = 30;}

else if(GroupCall.Session_Length__c == '45 mins'){

EventIns.DurationInMinutes = 45;}

else if(GroupCall.Session_Length__c == '1 hr'){

EventIns.DurationInMinutes = 60;}

else if(GroupCall.Session_Length__c == '8 hrs'){

EventIns.DurationInMinutes = 480;} 

 

EventIns.Subject = GroupCall.Call_Type__c;

EventIns.Description = GroupCall.Additional_Information__c;

EventIns.ownerID = UserInfo.getUserId();

EventList.add(EventIns);} 

insert AttendeeList;

insert EventList;} 

return (new ApexPages.StandardController(GroupCall)).view();} 

 

/* Set Search Criteria Values */

public void setName(String Name) {this.Name = Name;} 

public void setState(String State) {this.State = State;} 

public void setSpecialty(String Specialty) {this.Specialty = Specialty;} 

 

/* Apply Search Criteria Values to Query and Run Query */

public PageReference fullSearch() {

String queryName = '%' + Name + '%';

String queryState = '%' + State + '%';

String querySpecialty = '%' + Specialty + '%'; 

 

if (Name != '' && State != '' && Specialty != ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Account.Name LIKE :queryName ORDER BY Account.Name, LastName];} 

 

else if (Name == '' && State != '' && Specialty != ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE MailingState LIKE :queryState AND Specialty__c LIKE :querySpecialty ORDER BY Account.Name, LastName];} 

 

else if (Name != '' && State == '' && Specialty != ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Account.Name LIKE :queryName AND Specialty__c LIKE :querySpecialty ORDER BY Account.Name, LastName];} 

 

else if (Name != '' && State != '' && Specialty == ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Account.Name LIKE :queryName AND MailingState LIKE :queryState ORDER BY Account.Name, LastName];}

 

 else if (Name == '' && State == '' && Specialty != ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Specialty__c LIKE :querySpecialty ORDER BY Account.Name, LastName];} 

 

else if (Name == '' && State != '' && Specialty == ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE MailingState LIKE :queryState ORDER BY Account.Name, LastName];} 

 

else if (Name != '' && Name != null && ( (State == '') || (State == null) ) && ( (Specialty == '') || (Specialty == null) ) ){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Account.Name LIKE :queryName ORDER BY Account.Name, LastName];} 

 

else if (Name == '' && State == '' && Specialty == ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact ORDER BY Account.Name, LastName];} 

 

if (result.size() > 0)

SelectDisplay = true;

else

SelectDisplay = false;

return null; } 

 

public PageReference partSearch() {

String queryName2 = '%' + Name + '%';

String queryState2 = '%' + State + '%; 

 

if (Name != '' && State != ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Account.Name LIKE :queryName2 AND MailingState LIKE :queryState2 ORDER BY Account.Name, LastName]; } 

 

else if (Name == '' && State != ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE MailingState LIKE :queryState2 ORDER BY Account.Name, LastName]; }

 

else if (Name != '' && State == ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact WHERE Account.Name LIKE :queryName2 ORDER BY Account.Name, LastName]; }

 

else if (Name == '' && State == ''){

result = [SELECT Account.Name, Name, MailingState, Specialty__c, LastName FROM Contact ORDER BY Account.Name, LastName]; }  

if (result.size() > 0)

SelectDisplay = true;

else

SelectDisplay = false;

return null; }  

 

/* Create Attendee Redord for Selected Contacts */

public class contactwrapper {

public contact con{get; set;}

public Boolean selected {get; set;}

public contactwrapper(contact c){ 

con = c;selected = false;

}}} 

 

 Test Method (so far):

@isTestprivate class DBGExtensionTest { 

 

// Unit Test for general record creation

static testMethod void createGCTest() {

PageReference pageRef = Page.GroupCallPageDBG;

Test.setCurrentPage(pageRef);

contactSearchDBGExtension controller = new contactSearchDBGExtension();

Group_Call__c GroupCall = new Group_Call__c (Call_Type__c = 'Journal Club', CallStart__c = system.now(), Session_Length__c = '45 mins', State_National_ACtivity__c = 'State', PeopleTrained__c = '1 - 5'); 

controller.save();

system.assertEquals('Journal Club', GroupCall.Call_Type__c);

system.assertNotEquals(null, GroupCall.CallStart__c);

system.assertEquals('45 mins', GroupCall.Session_Length__c);

system.assertEquals('1 - 5', GroupCall.PeopleTrained__c); }  

 

// Unit Test for fullSearch pageRef

static testMethod void fullSearchTest1() { 

PageReference pageRef = Page.GroupCallPageDBG;

Test.setCurrentPage(pageRef);

contactSearchDBGExtension controller = new contactSearchDBGExtension();

controller.setName('PRM');

controller.setState('VIC');

controller.setSpecialty('General Practice');

List<Contact> l1 = controller.getResult();

controller.fullSearch();

system.assert( l1.size() == 0 );

controller.getConRes();

system.assertEquals( 'PRM', controller.getName());

system.assertEquals( 'VIC', controller.getState());

system.assertEquals( 'General Practice', controller.getSpecialty());

controller.allContactselected = true;

controller.allCheckboxes = true;

controller.getselectedContacts();

controller.saveSelectedContacts();

controller.save(); }  

 

// Unit Test for partSearch pageRef

static testMethod void partSearchTest() {

PageReference pageRef = Page.GroupCallPageDBG;

Test.setCurrentPage(pageRef);

contactSearchDBGExtension controller = new contactSearchDBGExtension();

controller.setName('PRM');

controller.setState('NSW');

List<Contact> l2 = controller.getResult();

controller.partSearch();

system.assert( l2.size() >= 0 );

controller.getConRes();

system.assertEquals( 'PRM', controller.getName());

system.assertEquals( 'NSW', controller.getState());

controller.allContactselected = true;

controller.getselectedContacts();

controller.saveSelectedContacts();

}

}

 

 
Message Edited by Doctor on 02-18-2010 05:56 AM
Message Edited by Doctor on 02-18-2010 05:57 AM
Message Edited by Doctor on 02-18-2010 05:59 AM
bob_buzzardbob_buzzard

allContactsSelected looks like a boolean that isn't used elsewhere in the code.  Setting this to true will cover the declaration, but as there is no code that takes action based on the value of this property, I wouldn't expect the coverage to change dramatically.

 

 

ColinKenworthy2ColinKenworthy2

Simply setting a boolean will not put anything into selectedContacts list so selectedContacts.size() will never be > 0 & hence nothing will be done.

You need to add items to the list programatically.

DoctorDoctor

Thanks Bob, I appreciate your response.

 

Most of this code was written by someone else, and I am pretty new to all of this.

Any suggestions as to how to write a test unit that will 'select' contacts from the search results and save? 

DoctorDoctor
Thanks Colin. How would I go about achieving this?
bob_buzzardbob_buzzard

You just need to add some elements to your selectedContacts list.  I haven't been through the code with a fine toothcomb, but it looks like the resultConList property stores all available contacts in a wrapper class format.

 

Thus in your test class, you'd need to iterate this list, and set the selected attribute of each element you wish to select to true.