0 votes
1 view
in Salesforce by (10k points)

I have created an Apex class but I was not able to implement a proper test class. Can anyone help me how exactly am I able to implement a proper test class? Herewith I have attached my apex class and test class.

Apex class.

public with sharing class AccountController {

@AuraEnabled

public static List<Account> findAll() {

    User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User

        where Id=:userinfo.getUserId() ];

    // Theme4t is theme that is used by mobille app for  android or iphone 

    if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non')

        || (userDetails.UserRole.Name).contains('go')) && UserInfo.getUiTheme() != 'Theme4t'){

       return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity

        FROM Account

        WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL 

        LIMIT:22000];

    }else {

       return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity

        FROM Account

        WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL 

        LIMIT:5000]; 

    }

}

Apex Test class.

@isTest 

public class AccountControllerTest 

{

static testMethod void testMethod1() 

            {

                           Account acc = new Account();

                           acc.Name='Test';

                           insert acc;

                           User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User

        where Id=:userinfo.getUserId() ];

                          List<Account> lstAcc = AccountController.findAll();

                          UserRole ur =new UserRole();

                         userDetails.UserRoleId=[select Id from UserRole where Name='yon'].Id;

               System.runAs(userDetails){

                          List<Account> lstAcc1 = AccountController.findAll();  

               }

               userDetails.UserRoleId=[select Id from UserRole where Name='bon'].Id;

               System.runAs(userDetails){

                          List<Account> lstAcc2 = AccountController.findAll();  

               }

                userDetails.UserRoleId=[select Id from UserRole where Name='non'].Id;

               System.runAs(userDetails){

                          List<Account> lstAcc3 = AccountController.findAll();  

               }

               userDetails.UserRoleId=[select Id from UserRole where Name='go'].Id;

               System.runAs(userDetails){

               List<Account> lstAcc4 = AccountController.findAll();  

         }                       

}

1 Answer

0 votes
by (20.3k points)

For the error, I have modify your test class and Apex class. Please find below for the code.

public with sharing class AccountController { 

            //using a test visible variable for setting the ui theme check.

            @TestVisible static Boolean isTheme4t = UserInfo.getUiThemeDisplayed() == 'Theme4t';

            @AuraEnabled

            public static List<Account> findAll() {

                User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User  where Id=:userinfo.getUserId()];

                // Theme4t is theme that is used by mobille app for  android or iphone 

                if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non') || (userDetails.UserRole.Name).contains('go')) && !isTheme4t){

                    return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity FROM Account WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL LIMIT 22000];

                }else {

                    return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity FROM Account WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL LIMIT 5000]; 

                }

            }

 }

    @isTest 

    public class AccountControllerTest 

    {

        //Use setup data method to create data and query it in testmethod 

        @testSetup static void setup() {

            UserRole r = new UserRole(DeveloperName = 'yon', Name = 'yon');

            insert r;

            User u = new User(

                ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,

                LastName = 'last',

                Email = '[email protected]',

                Username = '[email protected]' + System.currentTimeMillis(),

                CompanyName = 'TEST',

                Title = 'title',

                Alias = 'alias',

                TimeZoneSidKey = 'America/Los_Angeles',

                EmailEncodingKey = 'UTF-8',

                LanguageLocaleKey = 'en_US',

                LocaleSidKey = 'en_US',

                UserRoleId = r.Id

            );

            insert u;

            System.runAs(u){

                Account acc = new Account();

                acc.Name = 'Test Account';

                acc.ShippingLatitude = 75.46;

                acc.ShippingLongitude = 45.46;

                acc.AccountStatus__c = 'test';

                insert acc;

            }

        }

        static testMethod void testMethod1(){

            user u = [select Id from User where email = '[email protected]' limit 1];

            system.runAs(u){

                Test.startTest();

                List<Account> acc = [select Id,AccountStatus__c,ShippingLatitude,ShippingLongitude from Account where Name = 'Test Account'];

                List<Account> lstAcc4 = AccountController.findAll();  

                system.assert(lstAcc4.size()>0);

                Test.stopTest();

            }

        }

        static testMethod void testMethod2(){

            user u = [select Id from User where email = '[email protected]' limit 1];

            system.runAs(u){

                AccountController.isTheme4t = true;

                Test.startTest();

                List<Account> acc = [select Id,AccountStatus__c,ShippingLatitude,ShippingLongitude from Account where Name = 'Test Account'];

                List<Account> lstAcc4 = AccountController.findAll();  

                system.assert(lstAcc4.size()>0);

                Test.stopTest();

            }

        }

    }

I hope this will help. Want to know more about apex ? prefer this batch apex in salesforce

Want to become a Salesforce expert? join Salesforce Training now !!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...