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
Shawn ReichnerShawn Reichner 

Unexpected Token error when attempting to test a Test Class - Please Help!

Hello Experts!

I am trying to create a test apex class, which saves just fine, however when I attempt to actually run the test for the test class, i get the following error: Error Message System.QueryException: unexpected token: WHERE
Stack Trace Class.MyClosedIssuesTest.testMyClosedIssues: line 23, column 1

Here is my code, can anyone tell me what I may have done incorrectly, as google searching has yielded no results. 

@IsTest(SeeAllData=true)
public class MyClosedIssuesTest {

    static testMethod void testMyClosedIssues() {
        SObjectType objToken = Schema.getGlobalDescribe().get('Backlog__c');
        DescribeSObjectResult objDef = objToken.getDescribe();
        Map<String, SObjectField> fields = objDef.fields.getMap();
        Set<String> fieldSet = fields.keySet();

        String query = 'SELECT ';
        Integer counter = 0;
        for(String s : fieldSet) {
            counter++;
            SObjectField fieldToken = fields.get(s);
            DescribeFieldResult selectedField = fieldToken.getDescribe();
            query = query + selectedField.getName();
            if(counter < fieldSet.size()) {
                query = query + ', ';
            }
        }
        
        String query1 = query + 'FROM Backlog__c WHERE createdbyid = \'' + userinfo.getuserid() + '\'order by createddate desc';
        Backlog__c testIssue = Database.query(query1);
        
        
        
        testIssue.Parent__c = 'a3W180000008ddj';
        testIssue.Scheduled_Release__c = 'a3l18000000085c';
        testIssue.Environment__c = 'Production';
        testIssue.Summary__c = 'Add New Items To Account and Contact';
        testIssue.Description__c = 'Test';
        update testIssue;
        Backlog__c result = [Select ID,Description__c FROM Backlog__c WHERE Id = :testIssue.Id];
        
    }
}

Thank you in advance for any help you may be able to provide

Shawn
Best Answer chosen by Shawn Reichner
Parth ThakkarParth Thakkar
Shawn
after  update statement .
 

All Answers

Carlos Campillo GallegoCarlos Campillo Gallego
Hi Shawn,

Could it be that in line 22 where you write the query, you are typing two ' instead of one after the createdbyid ?

Regards
Shawn ReichnerShawn Reichner
It doesnt seems to be.....Once I remove one of the ' I get another error where line breaks are not allowed.. 

Thank you for the suggestion, is there any other suggested or variation of the suggestion you provided for me to attempt?

Thanks again,

Shawn
Shawn ReichnerShawn Reichner
I may be getting closer....I updated the class code to this...

@IsTest(SeeAllData=true)
public class MyClosedIssuesTest {

    static testMethod void testMyClosedIssues() {
        SObjectType objToken = Schema.getGlobalDescribe().get('Backlog__c');
        DescribeSObjectResult objDef = objToken.getDescribe();
        Map<String, SObjectField> fields = objDef.fields.getMap();
        Set<String> fieldSet = fields.keySet();

        String query = 'SELECT ';
        Integer counter = 0;
        for(String s : fieldSet) {
            counter++;
            SObjectField fieldToken = fields.get(s);
            DescribeFieldResult selectedField = fieldToken.getDescribe();
            query = query + selectedField.getName();
            if(counter < fieldSet.size()) {
                query = query + ', ';
            }
        }
        
        String query1 = query + 'FROM Backlog__c WHERE Parent__c != null LIMIT 1';
        Backlog__c testIssue = Database.query(query);
        Backlog__c testIss = [SELECT Id, Parent__c FROM Backlog__c WHERE Parent__c != null LIMIT 1];
        
        
        testIssue.Parent__c = 'a3W180000008ddj';
        testIssue.Scheduled_Release__c = 'a3l18000000085c';
        testIssue.Environment__c = 'Production';
        testIssue.Summary__c = 'Add New Items To Account and Contact';
        testIssue.Description__c = 'Test';
        update testIssue;
        Backlog__c result = [Select ID,Description__c FROM Backlog__c WHERE Id = :testIssue.Id];
        
    }
}

Now I am getting the following error - Error Message System.QueryException: unexpected token: '<EOF>'
Stack Trace Class.MyClosedIssuesTest.testMyClosedIssues: line 23, column 1

Can anyone please help me?  I am new at coding, so I am sure it is something simple, but I would appreciate any help!
Amit Chaudhary 8Amit Chaudhary 8
If found below issue in your test class.
1) Never use SeeAllData = true
2) Create  your test data inside your test class only. Then no need to excute Query again
3) If want to use above code only then please add space before from keyword.
4) Dont pass hard coded id in your test class. It will fail once you will try to deploye in other sandbox.
@IsTest()
public class MyClosedIssuesTest {

    static testMethod void testMyClosedIssues() 
	{
        Backlog__c testIssue = new Backlog__c();
        testIssue.Parent__c = 'a3W180000008ddj'; // please create parent record and add value here
        testIssue.Scheduled_Release__c = 'a3l18000000085c'; // please create Scheduled_Release__c record and add id here
        testIssue.Environment__c = 'Production';
        testIssue.Summary__c = 'Add New Items To Account and Contact';
        testIssue.Description__c = 'Test';
		insert testIssue;
		
        update testIssue;
        
    }
}
Please check below post. I hope that will help you in writing test classes.
1) Code Coverage Report in excel Format | Test Class Result in XLS
http://amitsalesforce.blogspot.com/2015/09/code-coverage-report-in-excel-format.html

2) Test classes with @isTest
http://amitsalesforce.blogspot.com/2015/09/test-classes-with-istest.html

3) Best Practice for Test classes | Sample Test class
http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

4) Salesforce Testing Best Practice Guidelines | Unit Test Mechanism in Salesforce | Test Methods
http://amitsalesforce.blogspot.com/2015/06/salesforce-testing-best-practice.html


Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
Parth ThakkarParth Thakkar
try to build query this way

String query;
  if(query == null)
       query = selectedField.getName();
   else
        query = query + ',' +selectedField.getName();

String Query1 ='Select '+ query +'FROM Backlog__c WHERE Parent__c != null LIMIT 1';

i thing your query not build correctly.
try an let me know.
Shawn ReichnerShawn Reichner
Thank you! I used your code below, however it did not provide any code coverage for the class I am attempting to test. Here is the class I am attempting to get code coverage for in order to promote to our production instance… public class MyClosedIssuesController { private ApexPages.StandardController controller; private PageReference page; public List issues1 {get; set;} public List issues2 {get; set;} public MyClosedIssuesController (ApexPages.StandardController stdController) { this.controller = stdController; this.page = ApexPages.currentPage(); SObjectType objToken = Schema.getGlobalDescribe().get('Backlog__c'); DescribeSObjectResult objDef = objToken.getDescribe(); Map fields = objDef.fields.getMap(); Set fieldSet = fields.keySet(); String query = 'SELECT '; Integer counter = 0; for(String s : fieldSet) { counter++; SObjectField fieldToken = fields.get(s); DescribeFieldResult selectedField = fieldToken.getDescribe(); query = query + selectedField.getName(); if(counter < fieldSet.size()) { query = query + ', '; } } String query1 = query + ' FROM Backlog__c WHERE createdbyid = \'' + userinfo.getuserid() + '\'order by createddate desc'; this.issues1 = Database.query(query1); String query2 = query + ' FROM Backlog__c WHERE createdbyid = \'' + userinfo.getuserid() + '\' AND status__c = \'Done\'order by createddate desc'; this.issues2 = Database.query(query2); } } Any help would be very much appreciated! Shawn Shawn Reichner Platform Developer, Salesforce AVI-SPL P: 866-588-6857 C: 813-347-7952 E: shawn.reichner@avispl.com [cid:E4FD79EE-D35C-4FF1-8BE7-F8259C061D08][cid:AB091AF6-9E7B-4283-B427-FF05A66C225A] Much appreciated!
Shawn ReichnerShawn Reichner
Thank you for the advise here, but after entering your code I get the selected field has a compile error Shawn Shawn Reichner Platform Developer, Salesforce AVI-SPL P: 866-588-6857 C: 813-347-7952 E: shawn.reichner@avispl.com [cid:424D8733-1BF5-462B-A681-6E41FEFF23D6][cid:59BFFD6A-864D-404A-9CEB-60C011EBFBCA]
Parth ThakkarParth Thakkar
call your controllar from test class then it will give you coverage

ApexPages.StandardController sc = new ApexPages.StandardController(  put here standard object's  instant that you use in page  );
 MyClosedIssuesController mc = new MyClosedIssuesController(sc);
Shawn ReichnerShawn Reichner

Amit,

After placing a space before the What, I now get a different error when attempting to test the test class.
System.QueryException: unexpected token: '<EOF>'

Any ideas there?  I am hopful you can help me, thanks in advance for any help you can provide,
 

Shawn

Shawn ReichnerShawn Reichner
Parth,

Here is the new test class....where are you advising me to place in the controller code you posted above?  Thank you and I apologize for being such a newbie here...

@IsTest()

public class MyClosedIssuesTest {




    static testMethod void testMyClosedIssues() 

    {

        Backlog__c testIssue = new Backlog__c();

        testIssue.Parent__c = 'a3W180000008ddj'; // please create parent record and add value here

        testIssue.Scheduled_Release__c = 'a3l18000000085c'; // please create Scheduled_Release__c record and add id here

        testIssue.Environment__c = 'Production';

        testIssue.Summary__c = 'Add New Items To Account and Contact';

        testIssue.Description__c = 'Test';

        insert testIssue;

        

        update testIssue;

        

    }

}
Parth ThakkarParth Thakkar
Shawn
after  update statement .
 
This was selected as the best answer
Shawn ReichnerShawn Reichner
Parth,

Any way I can get you to place it in there for me?  I just tried and I am getting a new error I have never seen before which is stating that line breaks are not allowed. 

Appreciate any additional help you can provide to help me through this
Shawn ReichnerShawn Reichner
Trying to Skype you now but it keeps timing out...do you wish to try to skype me sreichnersf