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
Taresh PandeyTaresh Pandey 

How can I make my test class executable.?

Hello All,
-:Apex Class:-

public class Innerclass4List 
{
    public class fourlists
    {
        public List<Integer> firstlist ;
        public List<Account> secondlist ;
        public List<Opportunity> thirdlist ;
        public List<String> fourthlist ;
    }
    public List<Account> selected (List<fourlists> FL)
    {
        return FL[0].secondlist;
    }  
}

-:Test Class:-

@isTest
public class testInnerclass4List 
{
    static testMethod void testmethodfourlist()
    {
        List<Innerclass4List.fourlists> testresultlist = new List<Innerclass4List.fourlists>();
           Innerclass4List.fourlists ICF = new Innerclass4List.fourlists();

            Account acct = new Account();
                
                   acct.Name = 'Ashok';
                acct.AnnualRevenue = 10000;
                acct.Phone = '0731467890';
        
                ICF.secondlist.add(acct);

                testresultlist.add(ICF);
       Innerclass4List ICL = new Innerclass4List();
       List<Account> secondlist = ICL.FL(testresultlist);
       system.assert(secondlist.size()>0);          
    }
}

Where I went wrong; I can't figure it out. I need your help. Please Let me know if you are getting error.

Thank you all in advance 
Best Answer chosen by Taresh Pandey
gautam_singhgautam_singh
Hi, 

I am not sure what you want to achieve functionally.

It seems that you have not initialzed the lists which are inside the wrapper class. Unless you initialse, memory would not be allocated at run time and hence I assume you are getting Attempt to De Reference. 

I updated your code excerpt and it is giving me 100% Code Coverage. Let me know if this worked.


Important :

If you have received the answer which you are looking for, Please mark it as a Solution, this will help others to find the resolution of problem with ease. More, you can make the Developers of the Community smile by appreciating the answer with the "Like" Button. We would love to help you again. 

Thank You
Gautam Singh 
Blog - http://singhgautam02.blogspot.in/
Me   - about.me/singhgautam
 
//apex controller 

public class Forum_TestClassCorrection {
	public class fourlists {
        public List<Integer> firstlist ;
        public List<Account> secondlist = new List<Account>();
        public List<Opportunity> thirdlist ;
        public List<String> fourthlist ;
    }
    
    public List<Account> selected (List<fourlists> FL){
        return FL[0].secondlist;
    }  
}

//test class 
@isTest
public class Forum_testInnerclass4List {

    static testMethod void testmethodfourlist(){
        //insert test account
        Account acct = new Account();
        acct.Name = 'Ashok';
        acct.AnnualRevenue = 10000;
        acct.Phone = '0731467890';
        insert acct;
        
        //ceate list of instances of wrapper class 
        List<Forum_TestClassCorrection.fourlists> listInstance = new List<Forum_TestClassCorrection.fourlists>();
        
        //create instance of wrapper class 
        Forum_TestClassCorrection.fourlists wrapperInstance = new Forum_TestClassCorrection.fourlists();
        
        //insert account in the wrapper instance 
        wrapperInstance.secondlist.add(acct);
        
        //insert wrapper instance in wrapper list
        listInstance.add(wrapperInstance);
        
        Test.startTest();
        //call the class 
        Forum_TestClassCorrection FT = new Forum_TestClassCorrection();
        //call specific method
        FT.selected(listInstance);
        Test.stopTest();
        
        
        
        
    }
        
}

 

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
I could notice you have not initialized 'secondlist' in the controller.
public List<Account> secondlist = new list<Account>();

Also, in the test class you have not inserted the test account record. 
Account acct = new Account();
                
                   acct.Name = 'Ashok';
                acct.AnnualRevenue = 10000;
                acct.Phone = '0731467890';
        insert acct;


 
gautam_singhgautam_singh
Hi, 

I am not sure what you want to achieve functionally.

It seems that you have not initialzed the lists which are inside the wrapper class. Unless you initialse, memory would not be allocated at run time and hence I assume you are getting Attempt to De Reference. 

I updated your code excerpt and it is giving me 100% Code Coverage. Let me know if this worked.


Important :

If you have received the answer which you are looking for, Please mark it as a Solution, this will help others to find the resolution of problem with ease. More, you can make the Developers of the Community smile by appreciating the answer with the "Like" Button. We would love to help you again. 

Thank You
Gautam Singh 
Blog - http://singhgautam02.blogspot.in/
Me   - about.me/singhgautam
 
//apex controller 

public class Forum_TestClassCorrection {
	public class fourlists {
        public List<Integer> firstlist ;
        public List<Account> secondlist = new List<Account>();
        public List<Opportunity> thirdlist ;
        public List<String> fourthlist ;
    }
    
    public List<Account> selected (List<fourlists> FL){
        return FL[0].secondlist;
    }  
}

//test class 
@isTest
public class Forum_testInnerclass4List {

    static testMethod void testmethodfourlist(){
        //insert test account
        Account acct = new Account();
        acct.Name = 'Ashok';
        acct.AnnualRevenue = 10000;
        acct.Phone = '0731467890';
        insert acct;
        
        //ceate list of instances of wrapper class 
        List<Forum_TestClassCorrection.fourlists> listInstance = new List<Forum_TestClassCorrection.fourlists>();
        
        //create instance of wrapper class 
        Forum_TestClassCorrection.fourlists wrapperInstance = new Forum_TestClassCorrection.fourlists();
        
        //insert account in the wrapper instance 
        wrapperInstance.secondlist.add(acct);
        
        //insert wrapper instance in wrapper list
        listInstance.add(wrapperInstance);
        
        Test.startTest();
        //call the class 
        Forum_TestClassCorrection FT = new Forum_TestClassCorrection();
        //call specific method
        FT.selected(listInstance);
        Test.stopTest();
        
        
        
        
    }
        
}

 
This was selected as the best answer
Taresh PandeyTaresh Pandey
Thank you Mr. Chandra Shekhar for your reply. I'm very new on this technology. but I have query "public class fourlists" is a inner class of class "Innerclass4List" and "Account" is a Standard object. How can I initialize Standard Object List into a inner class ? I know If I do that then Apex class will not generate error but test class would. I think initialization of List<Account> into inner class would be wrong. One more thing "insert" is a DML operation and as per I know TEST CLASS does not accept DML operation . I could be wrong mr. Chandra Shekhar but what do you say ?
Chandra Sekhar CH N VChandra Sekhar CH N V
Haven't heard that statement 'DML operation and as per I know TEST CLASS does not accept DML operation'. Setting up test records is the key for passing any test class. 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Since you are new to technology I would recommend you to go through trailhead modules (hope you have heard of this :-) )  or any developer workbooks/tutorails regarding apex.

 
Taresh PandeyTaresh Pandey
gAuTaM sInGh Thank you so much, 
But I wanna know one thing; do tell me why are you inserting account in the test class, I don't need insert operation in my main method. do you have another way for the same. 
gautam_singhgautam_singh
Hi, 

The Account getting inserted is not getting into the Database. I am inserting the record in the test class. 

Why do we need to insert record ? =  Why do we do in a Test Class ? 

Ans - Test classes are written to make an automated regrssive test coverage of your code. This makes sure that each of your conditions positive or negative are working properly. Now, in order to have code coverage of your code, you need to call your 'selected' method. 

This 'selected' method requies a parameter of a list of accounts (wrapper instance). to have this list of accounts, you need to insert a dummy test class account record. 

I believe you are completely new to test classes, I recommend you reading this (http://​https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods" target="_blank). This will make you aware of test classes more easily. 

 
Taresh PandeyTaresh Pandey
Chandra Shekhhar and Gautam Singh, your comments would be needful to me for sure. If I would face any other problem related to Apex Class I'll ask here again.