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
Tashika GuptaTashika Gupta 

test class for @remote action

here is my class


global class BT_Invoicing_New_Override_Crtl {

    public ApexPages.StandardController stdCon;
    public BT_Invoicing__c btinvoice{get;set;}
    public Static Integer BtjoblstSize;
    public Double Local_DFE_Quantity;
    public Double Local_Invoice_Cost_excl_vat;
    public String recordId;
    BT_Invoicing__c btinvoiceRecord;
    Public Static String btEstimateNumber;

    public BT_Invoicing_New_Override_Crtl(ApexPages.StandardController controller) 
    {
        stdCon= controller;
        btinvoice= (BT_Invoicing__c)stdCon.getRecord();
        recordId=ApexPages.currentPage().getParameters().get('id');
    } 
   
    public PageReference Save(){
    try{

        btinvoiceRecord= (BT_Invoicing__c)stdCon.getRecord();
       
        
            if(btinvoiceRecord.Invoice_Cost_excl_vat__c==null)
            Local_Invoice_Cost_excl_vat=0;
         else
            Local_Invoice_Cost_excl_vat=btinvoiceRecord.Invoice_Cost_excl_vat__c;
            
          if(btinvoiceRecord.DFE_Quantity__c==null)
             Local_DFE_Quantity=0;
          else
              Local_DFE_Quantity=btinvoiceRecord.DFE_Quantity__c;
            
     if(btinvoiceRecord.Pay__c=='Yes'&& ( Local_Invoice_Cost_excl_vat- Local_DFE_Quantity > 0)
                                         && btinvoiceRecord.Reject_Invoice__c==false && btinvoiceRecord.Invoice_In_Query__c==false)
          btinvoiceRecord.Invoice_Status__c='PAID-DFE';
     else if(btinvoiceRecord.Pay__c=='Yes'&& (Local_Invoice_Cost_excl_vat- Local_DFE_Quantity==0) 
                                         && btinvoiceRecord.Reject_Invoice__c==false && btinvoiceRecord.Invoice_In_Query__c==false)
          btinvoiceRecord.Invoice_Status__c='Paid';
     else if(btinvoiceRecord.Pay__c=='No' && btinvoiceRecord.Reject_Invoice__c==false && btinvoiceRecord.Invoice_In_Query__c==false)
          btinvoiceRecord.Invoice_Status__c='Active';
        
     
        
        upsert btinvoicerecord;
                
        PageReference pageref = new Pagereference('/'+btinvoicerecord.id);
        pageref.setRedirect(true);
        return pageref;
        
        return null;
      }
    catch(Exception ex){
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getDMLMessage(0));
        ApexPages.addMessage(msg);
        return null;
    }    
  }
  
  public PageReference SaveandNew(){
    try{
        stdCon.Save();  
        return new PageReference('/'+stdCon.getRecord().getSObjectType().getDescribe().getKeyPrefix()+'/e?');
         
      }
    catch(Exception ex){
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getDMLMessage(0));
        ApexPages.addMessage(msg);
        return null;
    }    
  
  }
  
  @RemoteAction
 Public Static Integer GetEstimateListsize(String btestimateno)
  {
         List<BT_Job__c> Btjoblst=[Select Id,Name,Estimate_No__c From BT_Job__c Where  Estimate_No__c=:btestimateno];
         BtjoblstSize=Btjoblst.size();
         System.debug('BtjoblstSize='+BtjoblstSize);
         return BtjoblstSize;
  }
  
   
}

n this is my test class

@istest
public class BT_Invoicing_New_Override_Crtl_Test {
    
     public static testmethod void myTestUnit1(){
      PageReference pageRef = Page.BT_Invoicing_New_Override;
        Test.setCurrentPage(pageRef);
        
        BT_Invoicing__c btInvObj = new BT_Invoicing__c();
        btInvObj.Invoice_Cost_excl_vat__c=55.20;
        btInvObj.DFE_Quantity__c=45.30;
        btInvObj.Pay__c='Yes';
        btInvObj.Reject_Invoice__c=false;
        btInvObj.Invoice_In_Query__c=false;
        
        insert btInvObj;
         
          ApexPages.StandardController sc = new ApexPages.standardController(btInvObj);
       BT_Invoicing_New_Override_Crtl sic = new  BT_Invoicing_New_Override_Crtl(sc);
      sic.Save(); 
     } 
     public static testmethod void myTestUnit2(){
      PageReference pageRef = Page.BT_Invoicing_New_Override;
        Test.setCurrentPage(pageRef);
        
        BT_Invoicing__c btInvObj = new BT_Invoicing__c();
        btInvObj.Invoice_Cost_excl_vat__c=45.30;
        btInvObj.DFE_Quantity__c=45.30;
        btInvObj.Pay__c='Yes';
        btInvObj.Reject_Invoice__c=false;
        btInvObj.Invoice_In_Query__c=false;
        insert btInvObj;
         
         
     
         ApexPages.StandardController sc = new ApexPages.standardController(btInvObj);
       BT_Invoicing_New_Override_Crtl sic = new  BT_Invoicing_New_Override_Crtl(sc);
      sic.Save(); 
     } 
     public static testmethod void myTestUnit3(){
      PageReference pageRef = Page.BT_Invoicing_New_Override;
        Test.setCurrentPage(pageRef);
        
        BT_Invoicing__c btInvObj = new BT_Invoicing__c();
        btInvObj.Invoice_Cost_excl_vat__c=00.00;
        btInvObj.DFE_Quantity__c=00.00;
        btInvObj.Pay__c='No';
        btInvObj.Reject_Invoice__c=false;
        btInvObj.Invoice_In_Query__c=false;
        insert btInvObj;
         
         
    
         ApexPages.StandardController sc = new ApexPages.standardController(btInvObj);
       BT_Invoicing_New_Override_Crtl sic = new  BT_Invoicing_New_Override_Crtl(sc);
      sic.Save(); 
     } 
    public static testmethod void myTestUnit4(){
      
       
        BT_Invoicing__c btInvObj = new BT_Invoicing__c();
        btInvObj.Invoice_Cost_excl_vat__c=10.00;
        btInvObj.DFE_Quantity__c=10.00;
        btInvObj.Pay__c='No';
        btInvObj.Reject_Invoice__c=false;
        btInvObj.Invoice_In_Query__c=false;
        insert btInvObj;
       BT_Invoicing__c btInvObj1=[select id , Invoice_Status__c from  BT_Invoicing__c where id=:btInvObj.id];
       
        PageReference pageRef = Page.BT_Invoicing_New_Override;
        Test.setCurrentPage(pageRef);
        Test.startTest();
      
      ApexPages.StandardController sc = new ApexPages.standardController(btInvObj1);
       BT_Invoicing_New_Override_Crtl sic = new  BT_Invoicing_New_Override_Crtl(sc);
      sic.SaveandNew();
      
        

       }
}

I need to increase the code coverage ,its nt covering remote action method and catch block
please help me out
Best Answer chosen by James Loghry
MithunPMithunP
Hi Tashika,

You should call @RemoteAction methods with class name.    (  ClassName.MethodName(Parameters);  )

In your case.

1. Create "BT_Job__c" record in test class like below, must add Estimate_No__c field.

BT_Job__c bt = new BT_Job__c(Estimate_No__c = '1234');
insert bt;

2. Create a string variable. //Note: above "Estimate_No__c" value and below "callingVar"value should be same  (i added 1234 in both places).

String callingVar = '1234';

3. Now call RemoteAction method like below

ClassName.MethodName(callingVar);


Best Regards,
Mithun.

All Answers

MithunPMithunP
Hi Tashika,

You should call @RemoteAction methods with class name.    (  ClassName.MethodName(Parameters);  )

In your case.

1. Create "BT_Job__c" record in test class like below, must add Estimate_No__c field.

BT_Job__c bt = new BT_Job__c(Estimate_No__c = '1234');
insert bt;

2. Create a string variable. //Note: above "Estimate_No__c" value and below "callingVar"value should be same  (i added 1234 in both places).

String callingVar = '1234';

3. Now call RemoteAction method like below

ClassName.MethodName(callingVar);


Best Regards,
Mithun.
This was selected as the best answer
Tashika GuptaTashika Gupta
hey Mithun,

Thanks a lot ,It worked
James LoghryJames Loghry
To be clear, @RemoteAction method are static methods by nature.  In other words, you're simply testing a @RemoteAction method the same way you would test any other static method, by calling ClassName.MethodName.
Angel30Angel30
Hi
could anybody please tell me how to write the test class for this.I am new in writing the test classes.
Kidnly help.
i tried writing by calling the methods but it tells variable doesnt exist. i think i am wrong somewhere.
help please.
Code snippet below



@RemoteAction
    public static String encryptDecryptUName(String uname, String action) {
        if(action == 'encrypt'){
            Blob cryptoKey = EncodingUtil.base64Decode(key);
            // Generate the data to be encrypted.  
            Blob data = Blob.valueOf(uname);
            // Encrypt the data and have Salesforce.com generate the initialization vector   
            Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
            String dataStr = EncodingUtil.base64Encode(encryptedData);
            return dataStr;
        }
        if(action == 'decrypt'){
            Blob cryptoKey = EncodingUtil.base64Decode(key);
            // Generate the data to be encrypted.  
            Blob data = EncodingUtil.base64Decode(uname);
            // Encrypt the data and have Salesforce.com generate the initialization vector   
            Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, data);
            String dataStr = decryptedData.toString();
            return dataStr;
        }
        return null;
        
    }