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
CharlieLangCharlieLang 

Apex testing problem

Hi All,

 

I am building some test code for some apex i created. When I have packaged everything up and deploying to another org i am getting an error as two of the fields i am referencing in the test code (TS_email__C and First_name__c) are being deployed as part of the package so they are not yet in the clients org.

 

Any help would be greatly appreciated

 

 

public static testMethod void tester()
    {
        User currentUserTest = [Select TS_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        System.runAs(currentUserTest)
        {
            buildit();
        }
    }

 

My OwnMy Own

 

 

Are you sending the User object fields in the package?,  if not include them with the test class while migration. 


CharlieLang wrote:

Hi All,

 

I am building some test code for some apex i created. When I have packaged everything up and deploying to another org i am getting an error as two of the fields i am referencing in the test code (TS_email__C and First_name__c) are being deployed as part of the package so they are not yet in the clients org.

 

Any help would be greatly appreciated

 

 

public static testMethod void tester()
    {
        User currentUserTest = [Select TS_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        System.runAs(currentUserTest)
        {
            buildit();
        }
    }

 



CharlieLang wrote:

Hi All,

 

I am building some test code for some apex i created. When I have packaged everything up and deploying to another org i am getting an error as two of the fields i am referencing in the test code (TS_email__C and First_name__c) are being deployed as part of the package so they are not yet in the clients org.

 

Any help would be greatly appreciated

 

 

public static testMethod void tester()
    {
        User currentUserTest = [Select TS_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        System.runAs(currentUserTest)
        {
            buildit();
        }
    }

 



CharlieLang wrote:

Hi All,

 

I am building some test code for some apex i created. When I have packaged everything up and deploying to another org i am getting an error as two of the fields i am referencing in the test code (TS_email__C and First_name__c) are being deployed as part of the package so they are not yet in the clients org.

 

Any help would be greatly appreciated

 

 

public static testMethod void tester()
    {
        User currentUserTest = [Select TS_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        System.runAs(currentUserTest)
        {
            buildit();
        }
    }

 





CharlieLangCharlieLang

Yeah. i'm including the 2 extra fields for the user object.

 

this is the error i'm getting

 

My OwnMy Own

 

 

This problem is becuase of no data in the "currentUserTest ", so try like this

//After SOQL. 
if(currentUserTest != null){
//Your code here.

}

 

CharlieLangCharlieLang

sorry for being really thick. where do i put it? I'm not particularly good at apex!

 

Here's the whole apex class.

 

thanks for your help in advance!!!!

 

public class IFA_search_controller
{

    public static String officepostcode { get; set; }

    public static String fsanumber { get; set; }

    public static String companyname { get; set; }

    public static String params { get; set; }

    public static String url { get; set; }

    public static String message { get; set; }
    
    public static void builduser()
    {
        message = '';
        User currentUser = [Select Touchstone_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        message += 'email:' + currentUser.Touchstone_email__c + ';';
        message += 'firstname:' + currentUser.First_name__c + ';';
    }
    
    public static void buildparams()
    {
        message += 'urn:000000;type:1;frn:' + fsanumber + ';name:' + companyname + ';pc:' + officepostcode + ';';
        params = message;
    }
    
    public static void encrypyt()
    {
        String algo = 'AES128';
        Blob pk = Blob.valueOf('lmv0Wdf8134kfq0j');             // public key
        Blob iv = Blob.valueOf('jkAsd98431jKsfd9');             // initialisation vector
        Blob ct = Blob.valueOf(message);                            // blob of message to encrypt
        Blob code = Crypto.encrypt(algo, pk, iv, ct);            // make the encrypted form
        String code1 = EncodingUtil.base64Encode(code);
        //now it's code1 we need to use in our URL, but shown here for convenience
        message = code1;
    }
    
    public static void buildurl()
    {   
        url = 'http://test.workload-online.co.uk/sfquery.aspx?query=' + message;
    }
    
    private static void buildit()
    {
        builduser();
        // next line lays down the sample data from the touchstone test records, replace with actual code to get fields from current record, etc.
        buildparams();
        encrypyt();
        buildurl();
    }

    public PageReference dosearch()
    {
        buildit();
        return null;
    }

    
    public static testMethod void tester()
    {
        User currentUserTest = [Select Touchstone_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        System.runAs(currentUserTest)
        {
            buildit();
        }
    }


}

 

My OwnMy Own
  public static void builduser()
    {
        try{
	   message = '';
	        User currentUser = [Select Touchstone_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       
        	
	   if(currentUser != null){
		message += 'email:' + currentUser.Touchstone_email__c + ';';
	        message += 'firstname:' + currentUser.First_name__c + ';';
	   }				

	}
	catch(Exception ex){
		
	   system.debug(ex.getMessage());
	
	}
    }
    

 try the above code. 

 

and test code as 

public static testMethod void tester()
    {
        User currentUserTest = [Select Touchstone_email__c, First_name__c from User where username = :UserInfo.getUserName() limit 1];       

if(currentUserTest != null){
	System.runAs(currentUserTest)
        {
            buildit();
        }
}        

}

 

CharlieLangCharlieLang

Ok. If i use the above, i only get a 70% code coverage. 

 

whats the best way of increasing this?

 

Thanks for your help with this!!!