Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Construct random strings of large sizes in your Apex tests

Construct random strings of large sizes in your Apex tests

Post by Ganesh Mathrubootham  (2012-01-23)

Status: Unverified
Level: intermediate

Problem

When writing Apex unit tests, you may have to generate strings of specific lengths to test boundary conditions. For example, let's say you have a server-side validation in place to check whether the user input for a text area field is 65,000 characters or less. If you want to write a unit test to test this piece of business logic written in Apex, you will have to construct strings which are exactly 65,000 and 64,999 characters long to test the boundary conditions (the first string is expected to fail the validation and the second one is expected to pass.) The problem here is how do you generate a string which contains exactly 65,000 characters? The brute force way of doing it would be to execute a for loop 65,000 times. The problem with this approach is that you would end up executing 65,000 code statements, which count toward the limit on the number of executed code statements. We need to employ a more efficient approach here.

Solution

The code sample below, a method that you can place in any class, is an efficient way to generate random strings of large size without executing too many code statements. This provides an O(log n) solution in terms of number of lines executed and can generate a string of size 100,000 by executing as few as 40 code statements.

    public static String constructTestString(Integer length) {
        String sample = 'abcde12345'; //10-digit string.
        
        for (Integer cutoff = 10; cutoff <= 1000000; cutoff = cutoff * 10) {
            if (length <= cutoff) {
                return sample.substring(0, length);
            }
            else {
                String newSample = '';
                for (Integer i = 0; i < 10; i ++) {
                    newSample += sample;
                }
                sample = newSample;
            }
        }
        
        return null;
    }

Starting with a string of size 10, this code sample concatenates the string to itself until the next power of 10 is reached. The concatenated string is used as the new sample string for the next iteration, which is to concatenate the new sample string to itself until the next power of 10 is reached. This continues until we hit a power of 10 that exceeds the expected length.

Discussion

This solution is good for generating long strings in your Apex tests.

  • If you need this across multiple tests, place this method in a common test utility class.
  • The above solution has been tested to work for strings of size up to 1 million characters. That should hopefully cover all your test scenarios. You can try it for bigger strings, but please be aware that you may run into other limits such as heap size.
  • This is an O(log n) solution. But there is still scope for improving the efficiency further such as stopping the concatenation as soon as the length is exceeded instead of waiting until the next iteration of the outer for loop.

Share

Recipe Activity - Please Log in to write a comment

Interesting solution. One possible issue that may be applicable in certain scenarios is that the generated string is not exactly random as it will be just repeated concatenation of "abcde12345". Anyhow this recipe presents an elegant way to generate large test strings of a large predefined size.

by Naveen Gabrani  (2012-01-26)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.