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
Michael Degn JensenMichael Degn Jensen 

Lookup user test class

Hi,

I starting out with unit testing of my first Apex Controller. I got a few test running, but this simple one is giving me trouble.

Apex Class:
public class ALTaskController {
    @AuraEnabled 
    public static user fetchUser(){
     // query current user information  
      User oUser = [select id,Name 
                 	FROM User
                    Where id =: userInfo.getUserId()];
        return oUser;
    }
}
Test class:
@isTest(SeeAllData=true)
private class ALTaskControllerTest {   
    @isTest static void testfetchUser() {
        User u = ALTaskController.fetchUser(0050N000007fvAwQAI);
        System.assertEquals('0050N000007fvAwQAI', u);
	}
   
}

I'm getting an error "Comparison arguments must be compatible types: String, User"

I have tried with string also, but fetchUser is expecting a User, so how to reference this?

Thanks,
Michael
Best Answer chosen by Michael Degn Jensen
Khan AnasKhan Anas (Salesforce Developers) 
Hi Michael,

Greetings to you!

You can use the below code:
@isTest
public class ALTaskControllerTest {
    
    @isTest
    public static void testfetchUser() {
        System.assertEquals(UserInfo.getUserId(), ALTaskController.fetchUser().Id, 'Got the right user');
    }
}

Please note that SeeAllData == true will give access to all records in your org. But it is always recommended and as per best practices, you should create the test records in the test classes instead of referring the existing records in org. 
Also, if you use the (SeeAllData=true), it might happen that test class pass in the sandbox but fails in production while deployment.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Michael,

Greetings to you!

You can use the below code:
@isTest
public class ALTaskControllerTest {
    
    @isTest
    public static void testfetchUser() {
        System.assertEquals(UserInfo.getUserId(), ALTaskController.fetchUser().Id, 'Got the right user');
    }
}

Please note that SeeAllData == true will give access to all records in your org. But it is always recommended and as per best practices, you should create the test records in the test classes instead of referring the existing records in org. 
Also, if you use the (SeeAllData=true), it might happen that test class pass in the sandbox but fails in production while deployment.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Michael Degn JensenMichael Degn Jensen
Hi Khan,
Thanks that makes sense and works like a charm!

And noted on the SeeAllData part.

Brgds,
Michael
Deepali KulshresthaDeepali Kulshrestha
Hi Michael,

You only need to call the class method it automatically covers your 100% code.
Try the following test class, it may be helpful for you:
​@IsTest
public class ALTaskController_Test {
    @IsTest
    public static void testfetchUser() {
        ALTaskController.fetchUser();
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha