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
Jezza83Jezza83 

Help with Test Methods

I have created test methods for my APEX code however I am unsure how to test the following which is still highlighted as uncovered.

Any help with how to go about this would be greatly appreciated !

public string fromUserID { get; set; } public string fromUserID1 { get; set; } public List<SelectOption> getfromUsers() { integer queryLimit = 5000; List<SelectOption> options = new List<SelectOption>(); options.add(new selectOption('', '--- select ---')); for (User u : [Select ID, Name FROM User Order by Name LIMIT :queryLimit]) { options.add(new selectOption(u.ID, u.Name)); } return options; } // Apply Search Criteria Values by User Input to SOQL Query public void search() { String queryFromUserID = FromUserID; String queryFromUserID1 = FromUserID1; // Run SOQL Query based on User Input if (FromUserID != null && FromUserID1 == null) { result = [ SELECT Name, Role_Type__c, Relationship_Manager_ID__c, Relationship_Manager_State__c, Account_Name__c, Account_State__c, Account_Number__c, Relationship_Manager_Name__c, Service_Type__c, Commencement_Date__c, LastModifiedDate, LastModifiedByID, OwnerID FROM Panel_Relationship__c WHERE Relationship_Manager_ID__c like :queryFromUserID ORDER by Account_Name__c ]; } else if (FromUserID == null && FromUserID1 != null) { result = [ SELECT Name, Role_Type__c, Relationship_Manager_ID__c, Relationship_Manager_State__c, Account_Name__c, Account_State__c, Account_Number__c, Relationship_Manager_Name__c, Service_Type__c, Commencement_Date__c, LastModifiedDate, LastModifiedByID, OwnerID FROM Panel_Relationship__c WHERE Account_Type__c like :queryfromUserID1 ORDER by Account_Name__c ]; }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard
Okay, I think I'm understanding a bit more now - is the intention of the testing code to set the fromUserID/fromUserID1 and then execute a search returning Panel_Relationship__c?
I reckon you want something along the following lines:
 

List<SelectOption> options=controller.getFromUsers();

System.assert(options.size()>0);

controller.fromUserID=options[0].getValue();
controller.fromUserID1=null;

List<Panel_Relationship__c> result=controller.search();

// do some asserts

// now test the other way around
controller.fromUserID=null;
controller.fromUserID1=options[0].getValue();

result=controller.search();

// do more asserts

 


Message Edited by bob_buzzard on 01-27-2010 01:42 AM

All Answers

Jezza83Jezza83

The test method I am trying to use is below, however it throws an error "Invalid Type: fromUserID", I have tried changing the fromUserID to fromUsers however I get the same error;

 

@istest static testMethod void fromUserstest() { //Use the PageReference Apex class to instantiate the page PageReference pageRef = Page.panel_maintenance_home_page; //The starting point of this test method. Test.setCurrentPage(pageRef); //Instantiate and construct the controller class. panelmaintanenceextension controller = new panelmaintanenceextension(); Panel_Relationship__c a = new Panel_Relationship__c() ; insert a; controller.fromUserID myclass = new controller.fromUserID(); List<Panel_Relationship__c> l = myclass.getfromUsers(); system.assert( l.size() > 0 ); } public List<Panel_Relationship__c> getfromUsers() { return [SELECT Id FROM Panel_Relationship__c limit 10 ]; } }

 

bob_buzzardbob_buzzard

On this line:

 

controller.fromUserID myclass = new controller.fromUserID()

 

You appear to be trying to declare a variable called myclass of type controller.fromUserID.  This isn't a valid type. fromUserID is a string property of the controller.

 

Looking at the code, I don't see that this line is adding anything, as you are just attempting to call fromUsers.  This is a method on the controller, so you should just be able to write:

 

controller.getfromUsers();  

 

However, this doesn't return a list of Panel_Relationship__c, so I'm not sure where that is coming from. 

Message Edited by bob_buzzard on 01-27-2010 01:34 AM
bob_buzzardbob_buzzard
Okay, I think I'm understanding a bit more now - is the intention of the testing code to set the fromUserID/fromUserID1 and then execute a search returning Panel_Relationship__c?
I reckon you want something along the following lines:
 

List<SelectOption> options=controller.getFromUsers();

System.assert(options.size()>0);

controller.fromUserID=options[0].getValue();
controller.fromUserID1=null;

List<Panel_Relationship__c> result=controller.search();

// do some asserts

// now test the other way around
controller.fromUserID=null;
controller.fromUserID1=options[0].getValue();

result=controller.search();

// do more asserts

 


Message Edited by bob_buzzard on 01-27-2010 01:42 AM
This was selected as the best answer
Jezza83Jezza83
Thanks again Bob - worked a charm