Hi I am new to Salesforce and I am trying to write test class for the Apex class.But I am not able to achieve it. Can someone help me? This is my apex class method.

    public static List<sObject> search(String searchTerm, string myObject, String filter) {
        System.debug('Inside Apex');
        String myQuery = null;
        if(filter != null && filter != ''){
            myQuery = 'Select Id, Name from '+myObject+' Where Name Like  \'%' + searchTerm + '%\' AND '+filter+' LIMIT  5';
        else {
                myQuery = 'Select Id, Name from '+myObject+' Where Name Like  \'%' + searchTerm + '%\' LIMIT  5';
        List<sObject> lookUpList = database.query(myQuery);
        System.debug('Inside Apex lookuplist'+lookUpList);
        return lookUpList;

This is my attempt to write the test class

public with sharing class Test_Custom_Lookup_Controller {
    static void shouldReturnaccObjectwithoutfilter(){
       Account acc = createAcc('Berlin');

   static void shouldReturnaccObjectwithfilter(){
       Account acc = createAcc('Berlin');

    private static Account createAcc(String name) {
       Account acc = new Account(Name = name);
        insert acc;
        return acc;
  • And what is your errors? What is not working?
    – Damecek
    Sep 23 at 9:29
  • @Damecek I am getting this error "System.QueryException: sObject type 'acc' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names." Sep 23 at 9:45
  • yes because no acc sobject exists in your sandbox, maybe you meant Account sobject
    – Damecek
    Sep 23 at 9:55
  • @Damecek Yes, how can I handle that. If i directly pass the object then also its giving an error. Sep 23 at 10:48
  • what error? share the message.
    – Damecek
    Sep 23 at 11:45

As has been discussed in the comments, this is an issue of bad test data/setup.

In unit tests, you are responsible for creating the test data and providing the appropriate inputs to the method you're testing. It's your job to craft the test environment so that your code executes in the manner you wish it to.

Sometimes, we want to craft a test environment that will cause some issue in executing the code (so-called "negative" test cases). That means that you'll perhaps not create a specific test record, or pass null into a method that expects an Integer. In all cases though, you should know exactly when you are setting up a bad environment, and you should also define what behavior you expect when the code you're testing receives bad data (maybe you ignore the issue and just return an empty list, maybe you throw an exception which your test can catch).

You're not working in a black box in this situation. You have access to the code that you're trying to test. So you are able to see how your inputs are used.

The two tests you have right now are "positive" tests (you're supposed to be providing appropriate and valid inputs).

myObject is used in the FROM clause in your query, so it must be a valid SObject name. "acc" is not a valid SObject name, but "Account" is.

Likewise, filter is used in the WHERE clause in your query, so it must be a valid filter expression. "ber" is not valid, but something like "CreatedDate = TODAY" would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.