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
newbiewithapexnewbiewithapex 

How to write test class to validate customer order?

I have this class as a controller for the customer order validate component . I need help writing test class for this one. Any kind of help would be much appreciate.
public class CustomerOrderValidateCntrl {
	
    @AuraEnabled
    public static UserNAccountDetails getParamDetails(String accId) {
        Account acc = getAccountDetails(accId);
        boolean profilecheckfailed = false;
        profilecheckfailed = isProfileNotToBeAllowed();
        system.debug('Tandem: '+acc.Tandem_Account_Number__c);
        return new UserNAccountDetails(profilecheckfailed,acc.Tandem_Account_Number__c,acc.Division_Number__c,getUserDetails());
    }
    public static Account getAccountDetails(String accId) {
        
        return [SELECT Id, Tandem_Account_Number__c, Division_Number__c
                       FROM Account
                       WHERE Id = :accId
                       LIMIT 1];        
    }
    public static String getUserDetails() {
        
        User usr = [SELECT Id, Network_Id__c
                    FROM User
                    WHERE Id = :UserInfo.getUserId()
                    LIMIT 1];
        
        return usr.Network_Id__c;        
    }  
    public static boolean isProfileNotToBeAllowed(){        
        SalesforceIds__c profileIds = SalesforceIds__c.getOrgDefaults();
        return (UserInfo.getProfileId().contains(profileIds.Profile_USF_ChefStoreRep__c));
    } 
    public class UserNAccountDetails{
        
        @AuraEnabled public Boolean profileCheckFailed {set;get;}
        @AuraEnabled public String tandemNumber {set;get;}
        @AuraEnabled public String divisionNumber {set;get;}
        @AuraEnabled public String networkId {set;get;}
        
        public UserNAccountDetails(Boolean profileCheckFailed, String tandemNumber, String divisionNumber, String networkId){
            this.profileCheckFailed=profileCheckFailed;
            this.tandemNumber=tandemNumber;
            this.divisionNumber=divisionNumber;
            this.networkId=networkId;
        }
    }
}
Best Answer chosen by newbiewithapex
Raj VakatiRaj Vakati
Its an issue with the missing the custom setting value insert in the test class and do it lilke below 
 
@isTest
private class CustomerOrderValidateCntrlTets {
 
    static testMethod void validateCustomer() {
      
	   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;
		System.runAs(uu){
			
			SalesforceIds__c  spo = new SalesforceIds__c() ; 
			spo.Profile_USF_ChefStoreRep__c= pd.Id ; 
			// insert all the values in to the custom settings  
			insert spo ; 
			
			
			
        Account account_Obj = new Account(Name = 'Test User', Tandem_Account_Number__c ='123123123', Division_Number__c='123123123');
        Insert account_Obj; 
		CustomerOrderValidateCntrl.getParamDetails(account_Obj.Id);
		}
		
	  
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this
@isTest
private class CustomerOrderValidateCntrlTets {
 
    static testMethod void validateCustomer() {
      
	   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;
		System.runAs(uu){
        Account account_Obj = new Account(Name = 'Test User', Tandem_Account_Number__c ='123123123', Division_Number__c='123123123');
        Insert account_Obj; 
		CustomerOrderValidateCntrl.getParamDetails(account_Obj.Id);
		}
		
	  
    }
}

 
newbiewithapexnewbiewithapex
Hi Thank for replying. I tried your code and it's giving me this errors(Line number might be different):

Class.CustomerOrderValidateCntrl.isProfileNotToBeAllowed: line 47, column 1
Class.CustomerOrderValidateCntrl.getParamDetails: line 16, column 1
Class.CustomerOrderValidateCntrlTest.validateCustomer: line 30, column 1

 MESSAGE 
System.NullPointerException: Argument cannot be null.
Raj VakatiRaj Vakati
Its an issue with the missing the custom setting value insert in the test class and do it lilke below 
 
@isTest
private class CustomerOrderValidateCntrlTets {
 
    static testMethod void validateCustomer() {
      
	   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;
		System.runAs(uu){
			
			SalesforceIds__c  spo = new SalesforceIds__c() ; 
			spo.Profile_USF_ChefStoreRep__c= pd.Id ; 
			// insert all the values in to the custom settings  
			insert spo ; 
			
			
			
        Account account_Obj = new Account(Name = 'Test User', Tandem_Account_Number__c ='123123123', Division_Number__c='123123123');
        Insert account_Obj; 
		CustomerOrderValidateCntrl.getParamDetails(account_Obj.Id);
		}
		
	  
    }
}

 
This was selected as the best answer
newbiewithapexnewbiewithapex
Thank you so much. Code coverage is 100% now.