1

I'm writing a test class and am testing different methods from the class that I am wanting to test. I'm testing an APEX class where most of the methods are lists, being populated by SOQL queries. To test these, I've been creating methods that create test data, and then at the end of the method, they call an instance of a class. Here's an example of what i'm talking about.

@isTest static void testgetProducts() {

    //Create new bundle
    Bundle__c b = new Bundle__c();
    b.Name = 'Test bundle';
    b.Experience__c = 'CX';
    b.Bundle__c = '0';
    b.Active__c = TRUE;
    insert b;

    //Create new bundle product
    Product_Bundle_Junction__c bp = new Product_Bundle_Junction__c();
    bp.Name = 'Test Bundle Product';
    bp.Related_Bundle__c = b.Id;
    insert bp;

    List<Product_Bundle_Junction__c> newquery = originalClass.getProductsMethod(b.Id);
}

And the parameter of the getProductsMethod is only passing in a string, so it'd look like this.

public static List<Product_Bundle_Junction__c> getProductsMethod(Id bundleId)

All the above code works great. Where I am stuck is I don't know in what format do I pass in a list as a parameter value? For example:

public static List<Product2> getSystemProductsMethod(list<string> productIds)

Instead of a string being passed in, a list of type string is being passed in. So what would the syntax be in the test class in calling that method and passing in a test list? I've tried...

List<Product2> newClassClass = originalClass.getSystemProductsMethod('Test List Item');

And that hasn't worked. I've tried other variations but those haven't worked. Do I need to create a list outside of my test method?

2

You can either create a List<String> (or List<Id>) variable in your test class and pass it by reference, like any other parameter, or you can use literal initialization syntax.

For the latter, it would look something like this:

public static List<Product2> getSystemProductsMethod(list<string> productIds);

@isTest
public static void testGetSystemProductsMethod() {
    MyClass.getSystemProductsMethod(new List<String>{
        'String1', 
        'String2', 
        ...
    });

    // assertions go here...
}

Note the use of curly braces rather than parentheses, like a constructor would use.

Also worth noting: if your methods are really accepting a List<Id>, declare it as such - not as a List<String>. If the values aren't Ids, naming the variable productIds may create extra confusion for you down the line.

  • That's exactly what I needed, that syntax worked perfectly. Thanks for answering that, I couldn't find that anywhere online. I appreciate it, David! – Kruse in Salesforce May 21 '18 at 13:42
1

If you're just trying to get the Ids as strings, you can simply use the following:

string bId = string.valueOf(b.Id); 

You can add those to to any list<string> that you declare.

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.