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
StaciStaci 

Test utility

Trying to write a Test Utility so I can call it in various test methods.  I've got this so far but getting an error
Constructors cannot be static at line 4 column 19

can anyone tell me what I'm doing wrong please?
@isTest
public class CW_DataCreation{

    public static CW_userDataCreation(){
     //create Tier 2 user
        Profile p = [SELECT Id FROM Profile WHERE Name='CW Software Support - Tier 2' limit 1]; 
        UserRole r = new UserRole(Name='CW Tier 2');
        User u1 = new User(LastName = 'Test Class',
                                    Username = 'TestClass@Test.com',
                                    UserRole = r,
                                    Profile = p,
                                    Email = 'Tier2Test@Test.com',
                                    Alias = 'tclass',
                                    CommunityNickname = 'tclass',
                                    TimeZoneSidKey = 'Asia/Kolkata',
                                    LocaleSidKey = 'en_US',
                                    EmailEncodingKey = 'UTF-8', 
                                    ProfileId = p.Id,
                                    LanguageLocaleKey = 'en_US',
                                    License_Owner__c = 'CW');
        insert u1;
        }
        return u1;
        }

 
Best Answer chosen by Staci
pconpcon
In addition to what Saikishore Reddy Aengareddy said, you also need to fix where your return is and insert your role object.
 
@isTest
public class CW_DataCreation{
    public static User CW_userDataCreation(){
        //create Tier 2 user
        Profile p = [
            selecT Id
            from Profile
            where Name='CW Software Support - Tier 2' 
            limit 1
        ];  

        UserRole r = new UserRole(Name='CW Tier 2');
        insert r;

        User u1 = new User(LastName = 'Test Class',
            Username = 'TestClass@Test.com',
            UserRole = r,
            Profile = p,
            Email = 'Tier2Test@Test.com',
            Alias = 'tclass',
            CommunityNickname = 'tclass',
            TimeZoneSidKey = 'Asia/Kolkata',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'UTF-8', 
            ProfileId = p.Id,
            LanguageLocaleKey = 'en_US',
            License_Owner__c = 'CW'
        );  
        insert u1;

        return u1;
    }   
}

 

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy
For the method you have defined there is no return type so the compiler would assume it as a constructor.

 
pconpcon
In addition to what Saikishore Reddy Aengareddy said, you also need to fix where your return is and insert your role object.
 
@isTest
public class CW_DataCreation{
    public static User CW_userDataCreation(){
        //create Tier 2 user
        Profile p = [
            selecT Id
            from Profile
            where Name='CW Software Support - Tier 2' 
            limit 1
        ];  

        UserRole r = new UserRole(Name='CW Tier 2');
        insert r;

        User u1 = new User(LastName = 'Test Class',
            Username = 'TestClass@Test.com',
            UserRole = r,
            Profile = p,
            Email = 'Tier2Test@Test.com',
            Alias = 'tclass',
            CommunityNickname = 'tclass',
            TimeZoneSidKey = 'Asia/Kolkata',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'UTF-8', 
            ProfileId = p.Id,
            LanguageLocaleKey = 'en_US',
            License_Owner__c = 'CW'
        );  
        insert u1;

        return u1;
    }   
}

 
This was selected as the best answer
StaciStaci
Thank you @pcon that worked.  One question.  When I call this in my test class, which has 3 methods, like this:
 User u1 = CW_DataCreation.CW_userDataCreation();

Works fine in the first method, but the second method its telling me I need to create unique usernames.  Is there a way to call it outside the 3 methods and use it in each method?  Otherwise I'll just create 3 users in my test utility