0
@AuraEnabled 
public static Integer generateRandomString() {
     try{
    return Math.round((Math.random() * (900000) + 100000));
    }
    catch (Exception e)
    {
        throw e;
    }
}
4

1 Answer 1

4
  1. Do some final polishing of your method. Do you want to call your method something with String, when it returns an Integer? Do you need an error handler? Which of the brackets do you really need?

  2. Implement the hull of your test class. Something like this:

@IsTest
private class CalculatorTest {

    @IsTest
    private static void generateRandomString() {
        Test.startTest();
        // TODO 1
        Test.stopTest();
        // TODO 2
        System.assert(true);
    }

    @TestSetup
    private static void testSetup() {
    }
}
  1. Ponder if you need anything in preparation of the test of your method. Does the database need to be set up? Do you need to know something about your environment, like record types, custom metadata types etc? In your case, - probably not.

  2. Call your method below TODO 1. Make sure you save the returned value to a variable.

  3. We are almost there. Think of the range of the output of your method. Think what you can be sure of. Do you expect values smaller than 0? Do you expect a million? Even something bigger? Change the "true" under TODO 2 to one of these cases. Compare the variable with -1, 1 000 000, something of your choice.

  4. Create copies of the test method generateRandomString(), each testing a different aspect of your returned value.

:-)

1
  • 1
    +1 : I like how you "teach a man to fish."
    – Moonpie
    Commented Feb 14, 2022 at 15:29

Not the answer you're looking for? Browse other questions tagged .