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
Edwin HerreraEdwin Herrera 

How do I test code?

Hi,
I am new to apex developer and I am having issues test the code I created for one of the modules. Below is the code I am attempting to test.

public class StringArrayTest {
    public static String[] generateStringArray(Integer i){
      String [] tests= new List<String>();
        for(Integer n=0;n<i;n++){
            tests.add('test '+n);
            }
        return tests;
    }
}

I click on run all tests and it states there are no tests. When I try to complete the challenge it tells me there is no class created.
Any help would be great.
Thank you.
 
DeveloperSudDeveloperSud
Hi Edwin,

Yes,you need to create a test class to test any apex class/trigger in salesforce. 
Please follow the below link.You will come to know how we can create a testclass in salesforce .
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Thanks!!!
Please comment if you find this helpfull.
LizzyLizzy
Hi Edwin,

I think the problem in your code is that Trailhead is looking for the output 'Test 1' etc, whereas your code returns 'test 1' with a lowercase t.
Changing the line 
tests.add('test '+n);
to 
tests.add('Test '+n);
should help!

Clicking run all tests does nothing at this point as you haven't actually written any unit tests. In Salesforce you're required to write code that tests your code - this test code is marked with an @isTest annotation and gets run when you click the button. There's definitely a module on unit testing on trailhead that's worth a run through!

Hope this helps,

Lizzy
DeveloperSudDeveloperSud
Hi Edwin,
You can refer the below test class.
@isTest
public class TestStringArrayTest {
    
    @isTest static void myTest(){
        
       List<String> stq= StringArrayTest.generateStringArray(10);
            
    }
}
Please comment if this helpful and mark this question as solved.Thanks.