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
ElectronElectron 

Unit Test for owner change trigger

I have a trigger works for changing the owner for related look-up object, and it works fine.

But I am confused by writint its test, I know test class shouldn't access a real data. 

 

So I try let test class insert a user, but user needs profile, that's still real data, if I do so, I still feel strange for this kind test. 

 

What I should do to test it? 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

You can't create Profile data in test method and each org will always have some profiles like Admin. So you can query data from profile using SOQL, that is totally okay.
You can use @isTest(seeAllData=false) to make sure you do not access org data.

All Answers

Bhawani SharmaBhawani Sharma

You can't create Profile data in test method and each org will always have some profiles like Admin. So you can query data from profile using SOQL, that is totally okay.
You can use @isTest(seeAllData=false) to make sure you do not access org data.

This was selected as the best answer
ElectronElectron

Sorry I forget to thank you. 

 

I also found this piece code, that I feel valued and store in code repository

 

        Profile pf = [select Id from Profile where Name='Standard User'];
        User testUser1 = new User(
        alias = 'test1', 
        email='test1@noemail.com',
        emailencodingkey='UTF-8', 
        lastname='Testing', 
        languagelocalekey='en_US',
        localesidkey='en_US', 
        profileid = pf.Id, 
        country='United States',
        timezonesidkey='America/Los_Angeles', 
        username='test1@noemail.com');
        insert testUser1;

 

Bhawani SharmaBhawani Sharma
You can kudoed me if you like :).