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
Damon LongDamon Long 

SOQL Query Test Class Issue

Hey all, 

I am having an issue with my test class I have created. I am only getting 44% code coverage with my test class and I am not sure why. Any help would be greatly appreciated.

Apex Class:
public class AssignedAdvisorController {
    @AuraEnabled
    public static User getAssignedAdvisor(Id recordId){
       Account account = [select Assigned_Adviser__c from Account where Id=: recordId];
        User user = [select Name, Phone from User where Id=: account.Assigned_Adviser__c];
        return User; 
       }  
    
    @AuraEnabled
    public static String getAdvisorPhone(Id recordId){
        Account account = [select Assigned_Adviser__c from Account where Id=: recordId];
        User user = [select Phone from User where Id=: account.Assigned_Adviser__c];
        String advisorPhone = user.Phone;
        return advisorPhone;
        
       }
}

Test Class:
@isTest
private class AssignedAdvisorControllerTest{
  @testSetup
  static void setupTestData(){
    test.startTest();
    User user_Obj = new User(FirstName = 'Test', LastName = 'User');
    Account account_Obj = new Account(Name = 'Test User', Assigned_Adviser__c = user_Obj.id);
    Insert account_Obj; 
    test.stopTest();
  }
  static testMethod void test_getAssignedAdvisor(){
     test.startTest();
    List<Account> account_Obj  =  [SELECT Id,Name,Assigned_Adviser__c from Account];
    System.assertEquals(true,account_Obj.size()>0);
    AssignedAdvisorController obj01 = new AssignedAdvisorController();
    AssignedAdvisorController.getAssignedAdvisor('01p90000006uk2w');
      test.stopTest();
  }
  static testMethod void test_getAdvisorPhone(){
      test.startTest();
    List<Account> account_Obj  =  [SELECT Id,Name,Assigned_Adviser__c from Account];
    System.assertEquals(true,account_Obj.size()>0);
    AssignedAdvisorController obj01 = new AssignedAdvisorController();
    AssignedAdvisorController.getAdvisorPhone('01p90000006uk2w');
      test.stopTest();
  }
}



 
Best Answer chosen by Damon Long
Raj VakatiRaj Vakati
@isTest
private class AssignedAdvisorControllerTest{
    @testSetup
    static void setupTestData(){
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        Account account_Obj = new Account(Name = 'Test User', Assigned_Adviser__c = uu.id);
        Insert account_Obj; 
        
    }
    static testMethod void test_getAssignedAdvisor(){
        System.Test.startTest();
        Account  account_Obj  =  [SELECT Id,Name,Assigned_Adviser__c from Account Limit 1];
        //ystem.assertEquals(true,account_Obj.size()>0);
        AssignedAdvisorController obj01 = new AssignedAdvisorController();
        AssignedAdvisorController.getAssignedAdvisor(account_Obj.Id);
        System.test.stopTest();
    }
    static testMethod void test_getAdvisorPhone(){
        System.test.startTest();
        Account  account_Obj  =  [SELECT Id,Name,Assigned_Adviser__c from Account Limit 1];
        // System.assertEquals(true,account_Obj.size()>0);
        AssignedAdvisorController obj01 = new AssignedAdvisorController();
        AssignedAdvisorController.getAdvisorPhone(account_Obj.Id);
        System.test.stopTest();
    }
}

 

All Answers

Raj VakatiRaj Vakati
@isTest
private class AssignedAdvisorControllerTest{
    @testSetup
    static void setupTestData(){
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        Account account_Obj = new Account(Name = 'Test User', Assigned_Adviser__c = uu.id);
        Insert account_Obj; 
        
    }
    static testMethod void test_getAssignedAdvisor(){
        System.Test.startTest();
        Account  account_Obj  =  [SELECT Id,Name,Assigned_Adviser__c from Account Limit 1];
        //ystem.assertEquals(true,account_Obj.size()>0);
        AssignedAdvisorController obj01 = new AssignedAdvisorController();
        AssignedAdvisorController.getAssignedAdvisor(account_Obj.Id);
        System.test.stopTest();
    }
    static testMethod void test_getAdvisorPhone(){
        System.test.startTest();
        Account  account_Obj  =  [SELECT Id,Name,Assigned_Adviser__c from Account Limit 1];
        // System.assertEquals(true,account_Obj.size()>0);
        AssignedAdvisorController obj01 = new AssignedAdvisorController();
        AssignedAdvisorController.getAdvisorPhone(account_Obj.Id);
        System.test.stopTest();
    }
}

 
This was selected as the best answer
Damon LongDamon Long
Thank you Raj, apparently I was writing my test classes completely wrong. I appreciate the help!