Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to cover wrapper class variables in apex test class?

Asked by Aalap Prabhakaran Apr 26, 2023 3.6K views 2 answers
Share

About this question

I am using a wrapper class (inner class) as below,

public without sharing class wmp_ctrl_NotificationHeader {

// other methods 
// 
 public class NotificationsWrapper{
        @AuraEnabled
        public List allNotifications; 
        @AuraEnabled
        public Integer unreadAlertSize;
    }
}

How can I cover test coverage for the wrapper class which has only these variables?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 28, 2024

Covering wrapper class variables in an Apex test class involves creating instances of your wrapper class within the test and ensuring all the properties and methods are exercised. Here's a step-by-step guide to help you achieve that:


1. Define Your Wrapper Class

A wrapper class in Apex is a custom class with various variables, often used to encapsulate complex data structures.

public class MyWrapper {
    public String name;
    public Integer value;
    public MyWrapper(String name, Integer value) {
        this.name = name;
        this.value = value;
    }
}2. Use the Wrapper Class in Your Main Class
Here’s an example of how your wrapper class might be used in a controller or service class.public class MyController {
    public List wrapperList { get; set; }
    public MyController() {
        wrapperList = new List();
        wrapperList.add(new MyWrapper('SampleName1', 10));
        wrapperList.add(new MyWrapper('SampleName2', 20));
    }
    public void addWrapper(String name, Integer value) {
        wrapperList.add(new MyWrapper(name, value));
    }
}

3. Write Test Class to Cover Wrapper Class Variables

  • To cover the wrapper class variables in your test class, you need to:
  • Create instances of your wrapper class.
  • Call the methods that utilize these wrapper class instances.

Here’s how you can write a test class for the MyController class to ensure the wrapper class variables are covered:

@isTest
public class MyControllerTest {
    @isTest
    static void testWrapperClass() {
        // Instantiate the controller
        MyController controller = new MyController();
        // Assert initial state of the wrapperList
        System.assertEquals(2, controller.wrapperList.size());
        System.assertEquals('SampleName1', controller.wrapperList[0].name);
        System.assertEquals(10, controller.wrapperList[0].value);
        System.assertEquals('SampleName2', controller.wrapperList[1].name);
        System.assertEquals(20, controller.wrapperList[1].value);
        // Add a new wrapper instance
        controller.addWrapper('SampleName3', 30);
        // Assert the new state of the wrapperList
        System.assertEquals(3, controller.wrapperList.size());
        System.assertEquals('SampleName3', controller.wrapperList[2].name);
        System.assertEquals(30, controller.wrapperList[2].value);
    }
}

Explanation

Creating Instances: The test method testWrapperClass first instantiates the MyController class, which in turn initializes the wrapperList with two MyWrapper instances.

Asserting Initial State: The test method uses System.assertEquals to verify the initial state of the wrapperList to ensure the constructor and initial setup work correctly.

Adding a Wrapper Instance: The test method calls addWrapper to add another MyWrapper instance to the list.

Asserting New State: Finally, the test method asserts the new state of the wrapperList to confirm that the addWrapper method works as expected.

Conclusion

By following these steps, you ensure that all variables and methods related to your wrapper class are covered in your Apex test class. This not only improves your code coverage but also verifies the correct behavior of your class methods and initializations.


Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.