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
kiran k 12kiran k 12 

test class for remoteaction method

Need test class for @remoteaction.i wrote a test class it gives 45%,but it cannot cover remoteaction method.can anybody help me how to cover remoteaction method in test class?
this is my class
global class RFLEET_ProtocolLineController 
{
//declartion part
    public Boolean refreshPage { get; set; }
    public string volumeJSON {get;set;} 
    public string newlineinsertlist{get;set;}
    List<String> v1= new List<String>();
    List<List<String>> v2= new List<List<String>>();
    List<String> v3= new List<String>();
    List<List<String>> v4= new List<List<String>>();
    string Id;
    public String gridtype{get;set;}
    public String gridname{get;set;}
    public boolean hideExcel{get;set;}
    public boolean hidetable2{get;set;}
    public RFLEET_ProtocolLineController(ApexPages.StandardController controller) {
    hideExcel=true;
    hidetable2=false;
    id=ApexPages.currentPage().getParameters().get('id');
    RFLEET_Protocol_Grid__c grid=[select id,name,Rfleet_Type_of_Grid__c from RFLEET_Protocol_Grid__c where id=:id];
    gridtype=grid.Rfleet_Type_of_Grid__c;
    gridname=grid.id;
    Rfleet_ProtocolLineList();
}      
 
//copy-paste solution existing values fetching from protocol lines    

public void Rfleet_ProtocolLineList(){  

    v2= new List<List<String>>();
    for(RFLEET_Protocol_Line__c oli: [select id,Rfleet_Discount__c,Rfleet_Model__c,Rfleet_Option__c,Rfleet_Price__c,Rfleet_PricePercent__c ,Rfleet_Protocol_grid_name__c,Rfleet_Version__c ,Rfleet_Volume__c from RFLEET_Protocol_Line__c where  Rfleet_Protocol_grid_name__c=:gridname ]) { 
    v1= new List<String>();   
    v1.add(oli.id);             
    v1.add(oli.Rfleet_Model__c);
    v1.add(oli.Rfleet_Option__c);
    v1.add(string.valueOf(oli.Rfleet_Price__c));
    v1.add(String.valueOf(oli.Rfleet_Discount__c));
    v1.add(String.valueOf(oli.Rfleet_PricePercent__c));
    v1.add(oli.Rfleet_Version__c);
    v1.add(String.valueOf(oli.Rfleet_Volume__c));
    v1.add(oli.Rfleet_Protocol_grid_name__c);
    v2.add(v1);
    }
    volumeJSON= JSON.serialize(v2);  
}
 
//Rfresh table and redirect to home page protocol grids    
public void cancel(){

    hideExcel=true;
    hidetable2=false;
    refreshPage =true;
}

//copy-paste solution new line insert function empty values passing  here
public void RfleetnewlineExcel(){  
    hideExcel=false;
    hidetable2=true;
    RFLEET_Protocol_Line__c ww=new RFLEET_Protocol_Line__c();
    
    for(integer i=1; i<=20;i++){
        String model=ww.Rfleet_Model__c;
        String Option=ww.Rfleet_Option__c;
        String Price=(string.valueOf(ww.Rfleet_Price__c));
        String Discount=(String.valueOf(ww.Rfleet_Discount__c));
        String Version=ww.Rfleet_Version__c;
        String Volume=(String.valueOf(ww.Rfleet_Volume__c));
        String PricePercent=(String.valueOf(ww.Rfleet_PricePercent__c));
        String masterid=ww.Rfleet_Protocol_grid_name__c;
        v3= new List<String>();   
        v3.add(model);
        v3.add(Option);
        v3.add(Price);
        v3.add(Discount);
        v3.add(Version);
        v3.add(Volume);
        v3.add(PricePercent);
        v4.add(v3); 
       
    }
    newlineinsertlist= JSON.serialize(v4); 
     
}
   
  
//copy-paste solution update function starts here
@RemoteAction  
global static pagereference AccSave(List<List<String>> valuefromJS){
   
    List<RFLEET_Protocol_Line__c> volSave = new List<RFLEET_Protocol_Line__c>();
   
    for(Integer i=0; i<valuefromJS.size(); i++){
        RFLEET_Protocol_Line__c v=new RFLEET_Protocol_Line__c();

        List<String> l1 = valuefromJS[i];
        
        v.id=l1[0];
        v.Rfleet_Model__c=l1[1];
        
        if(l1[2] != null ){ 
        v.Rfleet_Option__c= string.valueof(l1[2]);
        }
        
        
       if(!string.isBlank(l1[3])){ 
        v.Rfleet_Price__c= Decimal.valueof(l1[3]);
         }
      else{
        v.Rfleet_Price__c=null;
        }
       
        
         if(!string.isBlank(l1[4])){ 
        v.Rfleet_Discount__c=Decimal.valueof(l1[4]);
        }else{
        v.Rfleet_Discount__c=null;
        }
        
         if(!string.isBlank(l1[5])){ 
        v.Rfleet_PricePercent__c=Decimal.valueof(l1[5]);
        }else{
        v.Rfleet_PricePercent__c=null;
        }      
        
        if(l1[6] != null ){
        v.Rfleet_Version__c=string.valueof(l1[6]);
        }
         if(!string.isBlank(l1[7])){ 
        v.Rfleet_Volume__c=Decimal.valueof(l1[7]);
        }else{
        v.Rfleet_Volume__c=null;
        } 
        v.Rfleet_Protocol_grid_name__c=l1[8];
        
        volSave.add(v);
        
    }
    
    update volSave; 
   return null;
    
}
 
 
 // copy-paste solution action stared for newline insertion 
 @RemoteAction  
   global static pagereference newlineSave(List<List<String>> valuefromJSNewdata,string Masterid,string gtype ){ 
   
       List<RFLEET_Protocol_Line__c> volSave1 = new List<RFLEET_Protocol_Line__c>();
       
        for(Integer i=0; i<valuefromJSNewdata.size(); i++){
        
                RFLEET_Protocol_Line__c proline=new RFLEET_Protocol_Line__c();
                              
                List<String> l2 = valuefromJSNewdata[i];
                
                system.debug('<<<<<'+l2[0]);
                system.debug('<<<<<'+l2[1]);
                if(l2[0]==null && l2[1]==null && l2[2]==null && l2[3]==null && l2[4]==null && l2[5]==null && l2[6]==null)
                 {
                 system.debug('<<<<inside if null values');
                 }
                 else{
                       system.debug('<<<<inside else with values');    
                       
                       
                proline.Rfleet_Model__c=l2[0];
                if(l2[1] != null ){ 
                proline.Rfleet_Option__c= string.valueof(l2[1]);
                }
                if(l2[2] != null ){ 
                proline.Rfleet_Price__c= Decimal.valueof(l2[2]);
                }
                if(l2[3] != null ){
                proline.Rfleet_Discount__c=Decimal.valueof(l2[3]);
                }
                if(l2[6] != null ){
                proline.Rfleet_PricePercent__c=Decimal.valueof(l2[6]);
                }   
                if(l2[4] != null ){
                proline.Rfleet_Version__c=string.valueof(l2[4]);
                }
                if(l2[5] != null ){
                proline.Rfleet_Volume__c=Decimal.valueof(l2[5]);
                }
                proline.Rfleet_Protocol_grid_name__c=Masterid;
                
                volSave1.add(proline);
                  
            } 
                         
     }
      insert volSave1;
     
       return null;  
  } 
  

    
}
test class for above class:
@isTest
public class RFLEET_ProtocolLineController_Test{
    public static testMethod void copy(){
    
            RFLEET_Protocol_Grid__c din = new RFLEET_Protocol_Grid__c(Name='sand');
            insert din;
            din.Name = 'sanc';
            update din;
            system.debug('din.id'+din.id);   
            ApexPages.currentPage().getParameters().put('id',din.id);
            ApexPages.StandardController sc = new ApexPages.standardController(din);
            RFLEET_ProtocolLineController rp = new RFLEET_ProtocolLineController(sc);
            string selcontactid;
            selcontactid = System.currentPagereference().getParameters().get('conid');
            Account Acc= New Account(Name='A1',Montant__c=1.4);
            insert Acc;
    
        RFLEET_Protocol__c Rpro=new RFLEET_Protocol__c(Name='Aggre',Rfleet_Agreement_Client__c=Acc.id,Rfleet_Status__c='Actif',Rfleet_Billing_Type__c='Dossier');
            insert Rpro;
    
            RFLEET_Protocol_Grid__c pg=new RFLEET_Protocol_Grid__c(Name='t1',Rfleet_Type_of_Grid__c='Model - Discount %',Rfleet_Type_of_Sales__c='VD');
            insert pg;
       
             RFLEET_Protocol_Line__c  record = new RFLEET_Protocol_Line__c(Rfleet_Model__c='model',Rfleet_Discount__c=1.2,Rfleet_Version__c='test',Rfleet_Option__c='test',Rfleet_Volume__c=1,Rfleet_Price__c=12,Rfleet_Protocol_grid_name__c=pg.id);
             insert record;
            record.Rfleet_Model__c='test';
            update record; 
        
        rp.cancel();
        rp.Rfleet_ProtocolLineList();   
        rp.RfleetnewlineExcel();
        list<string> str=new list<string>();


 
Alexander TsitsuraAlexander Tsitsura
Hi Kiran,

You can view uncoverage lines, for it use developer console or ide. 
For developer console read documantation https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en (https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en)

User-added image

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex