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
Harsha ShriHarsha Shri 

Need help in Test class for wrapper class

Hi All,
I need help in writing test class for wrapper class. This is my class
public class Wrapper{

  
  
  public class Item {
  
     public Boolean checked{ get; set; }
     public Sobject sobj { get; set;}
     public O2r__x O1{ get; set;}
     public O1r__x O2{ get; set;}
     public String Type{ get; set;}
     public String ExternalID{ get; set;}
     public String id{ get; set;}
     public String sname{ get; set;}
     public Double net{ get; set;}
    
     
     public Item(Sobject so,String type){
        sobj=so;
        if(type=='chp'){
        O2=(O1r__x)so;
        ExternalID=O2.ExternalID;
        id=O2.id;
        sname=O2.sname__c;
       net=O2.net__c;
       
        }
        if(type=='odr'){
        O1=(O2r__x)so;
        ExternalID=O1.ExternalID;
        id=O1.id;
        sname=O1.sname__c;
        
        }
       
        this.Type=type;
        checked = false;
    } 
}
}

Please help me with test class

Thank you very much​
Best Answer chosen by Harsha Shri
v varaprasadv varaprasad
Hi Harsha,

Unfortunately, it doesn't seem like you can test this the way you normally would.  I was talking with Pat Patterson [1][2] and the only way to do this is to carefully wrap your remote object queries with isRunningTest and return data there.  The ability should be "coming soon."

[1] https://twitter.com/metadaddy/status/659074056369500160
[2] https://twitter.com/metadaddy/status/659075008757567488

 
public class Wrapper{

  
  
  public class Item {
  
     public Boolean checked{ get; set; }
     public Sobject sobj { get; set;}
     public O2r__x O1{ get; set;}
     public O1r__x O2{ get; set;}
     public String Type{ get; set;}
     public String ExternalID{ get; set;}
     public String id{ get; set;}
     public String sname{ get; set;}
     public Double net{ get; set;}
    
     
     public Item(Sobject so,String type){
        sobj=so;
	    if(Test.isRunningTest()){
        if(type=='chp'){
        O2=(O1r__x)so;
        ExternalID=O2.ExternalID;
        id=O2.id;
        sname=O2.sname__c;
       net=O2.net__c;
         }
        }
        if(type=='odr'){
		if(Test.isRunningTest()){
        O1=(O2r__x)so;
        ExternalID=O1.ExternalID;
        id=O1.id;
        sname=O1.sname__c;
         }
        }
       
        this.Type=type;
        checked = false;
    } 
}
}

@isTest
public class Wrapper_Test{
  public static testmethod void testItemClass(){
     public Boolean checked;
     public Sobject sobj = new Sobject();
     public O2r__x O1;
     public O1r__x O2;
     public String Type1 = 'chp';
	 public String Type2 = 'odr';
     public String ExternalID;
     public String id;
     public String sname;
     public Double net;
	 
	 Wrapper.Item ai1 = new Wrapper.Item(sobj,Type1);
	 ai1.checked = False;
	 ai1.ExternalID= 'ExId';
	 ai1.id = 'id';
	 ai1.sname = 'TestData';
	 ai1.net = 2.0;
	 
	 Wrapper.Item ai2 = new Wrapper.Item(sobj,Type2);  
  
  }


}
More Info:
https://developer.salesforce.com/forums/?id=906F00000005HkFIAU

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 

All Answers

v varaprasadv varaprasad
Hi Harsha,

Unfortunately, it doesn't seem like you can test this the way you normally would.  I was talking with Pat Patterson [1][2] and the only way to do this is to carefully wrap your remote object queries with isRunningTest and return data there.  The ability should be "coming soon."

[1] https://twitter.com/metadaddy/status/659074056369500160
[2] https://twitter.com/metadaddy/status/659075008757567488

 
public class Wrapper{

  
  
  public class Item {
  
     public Boolean checked{ get; set; }
     public Sobject sobj { get; set;}
     public O2r__x O1{ get; set;}
     public O1r__x O2{ get; set;}
     public String Type{ get; set;}
     public String ExternalID{ get; set;}
     public String id{ get; set;}
     public String sname{ get; set;}
     public Double net{ get; set;}
    
     
     public Item(Sobject so,String type){
        sobj=so;
	    if(Test.isRunningTest()){
        if(type=='chp'){
        O2=(O1r__x)so;
        ExternalID=O2.ExternalID;
        id=O2.id;
        sname=O2.sname__c;
       net=O2.net__c;
         }
        }
        if(type=='odr'){
		if(Test.isRunningTest()){
        O1=(O2r__x)so;
        ExternalID=O1.ExternalID;
        id=O1.id;
        sname=O1.sname__c;
         }
        }
       
        this.Type=type;
        checked = false;
    } 
}
}

@isTest
public class Wrapper_Test{
  public static testmethod void testItemClass(){
     public Boolean checked;
     public Sobject sobj = new Sobject();
     public O2r__x O1;
     public O1r__x O2;
     public String Type1 = 'chp';
	 public String Type2 = 'odr';
     public String ExternalID;
     public String id;
     public String sname;
     public Double net;
	 
	 Wrapper.Item ai1 = new Wrapper.Item(sobj,Type1);
	 ai1.checked = False;
	 ai1.ExternalID= 'ExId';
	 ai1.id = 'id';
	 ai1.sname = 'TestData';
	 ai1.net = 2.0;
	 
	 Wrapper.Item ai2 = new Wrapper.Item(sobj,Type2);  
  
  }


}
More Info:
https://developer.salesforce.com/forums/?id=906F00000005HkFIAU

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 
This was selected as the best answer
v varaprasadv varaprasad
External objects will not support for test class O2r__x!..