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

Test for String Query

I got a public apex class with the following query in it: 
List<Case> jobs;
  String query='select Id, Account.Name, Time__c, End__c, Status, Type, CaseNumber from Case WHERE Time__c != null AND End__c != null';
  String whereStr='';
And below is my test class: 
class CaseListControllerTest {
    static testMethod void testCaseList (){
    List<Case> cases = new List<Case>();
    for (Integer count = 0; count < 50; count++) {
        cases.add(new Case (Subject='Test', Status = 'Open'));
    insert cases;
    Case ACC = [select phone from Account LIMIT 1];
        System.assertEquals ( String.valueOf(ACC.Subject) ,'Test');

Currently i am receiving: Compile Error: Method does not exist or incorrect signature: CalendarController.findAll() at line 12 column 9

But how would i test for the string query? 

Please share CalendarController code.
manoj krishnamanoj krishna
You are calling "CalendarController.findAll()" method in test call, and from the above code you have shared there is no method called findAll in the CalendarController apex class. Hence you are getting the above error
Glyn Anderson 3Glyn Anderson 3
The code you're trying to test is in the pageLoad method.  In your test, you will have to instantiate a CalendarController instance and invoke the pageLoad method.  Note though, that none of the Cases you create in your test will satisfy the WHERE clause of the query.  Also, I don't think the query is going to work, because you're going to have the word 'WHERE' in it more than once.  I'm also wondering why "CaseListControllerTest" is testing "CalendarController".  Does CalendarController work in your VisualForce page?  I can help you clean this up if I can better understand what you are trying to do.
Glyn Anderson 3Glyn Anderson 3
I would very much discourage you from trying to use a remote action - I don't see how it will help anything.  If the controller works, then you should not modify it for the sake of the test.

In your test code, you need to create some case records that meet the criteria of your query (Time__c != null AND End__c != null).  The cases your test creates now do not meet those criteria.  You don't need 50 records - just enough to create a valid test.  Make sure the cases you create have different values for Status and Booked_Tech__c, so that all paths through the pageLoad method are taken.  You might want to create one that does not meet the criteria, so that you can assert that it was not queried.

Since there is no 'findAll()' method, there is no reason to test it.  Your test code should call the 'pageLoad()' method and then assert that you have the correct data in the 'events' list.

In your controller, it looks like 'tmpJob' is used to alter the filter settings.  Have you tested the controller with the alternate filter values?  My concern is that when you concatenate 'query' and 'whereStr', you will get a malformed query.  For example:

select Id, Account.Name, Time__c, End__c, Status, Type, CaseNumber from Case WHERE Time__c != null AND End__c != nullWHERE Account.Name = :clientId AND Booked_Tech__c = :techId

Line 34 shouldn't compile as shown.  It should be:

jobs = (List<Case>) Database.query(query +whereStr);

Let me know if you have questions.
Glyn Anderson 3Glyn Anderson 3
One more issue:

'WHERE Account.Name = :clientId'

won't do what you want.  It should be:

'WHERE AccountId = :clientId'
Glyn Anderson 3Glyn Anderson 3
Are you still having problems with this?  If any of these posts helped you solve the problem, please mark it as the Best Answer.  If you solved it another way, please post your solution to help others with a similar problem.  You can mark your own post as the Best Answer.  Thanks!