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
vinod kumar 794vinod kumar 794 

{Get; Set;} test method hello i write a test method for a controller.but i get only 71% code coverage. i am facing problem to write the test method for get set method.please help me here is my code

hello

   i write a test method for a controller.but i get only  71% code coverage.

  i am facing  problem to write the test method for get set method.please help me

 here is my code:
public class eiException extends exception{
    public String code{get; set;}
    public String text{get; set;}
    public String severity{get; set;}
    public String category{get; set;}
    public String subcategory{get; set;}
    public String dataState{get; set;}
    public Boolean retryAllowed{get; set;}
    public String additionalInformation{get; set;}
    
    public eiException(Dom.XmlNode node){
        dom.XmlNode p;
        
        p=node.getChildElement('code', eiEnvelope.namespace);
        if(p != null){code = p.getText();}
        
        p=node.getChildElement('text', eiEnvelope.namespace);
        if(p != null){text = p.getText();}
        
        p=node.getChildElement('severity', eiEnvelope.namespace);
        if(p != null){severity = p.getText();}
        
        p=node.getChildElement('category', eiEnvelope.namespace);
        if(p != null){category = p.getText();}
        
        p=node.getChildElement('subcategory', eiEnvelope.namespace);
        if(p != null){subcategory = p.getText();}
        
        p=node.getChildElement('dataState', eiEnvelope.namespace);
        if(p != null){dataState = p.getText();}
        
        p=node.getChildElement('retryAllowed', eiEnvelope.namespace);
        if(p != null){retryAllowed = p.getText().equalsIgnoreCase('true');}
        
        p=node.getChildElement('additionalInformation', eiEnvelope.namespace);
        if(p != null){additionalInformation = p.getText();}
        
    }
    
    public static testmethod void testeiException() {
    
        
        DOM.Document doc = new DOM.Document();
        dom.XmlNode envelope= doc.createRootElement('Envelope',null,null);
        dom.XmlNode body= envelope.addChildElement('code', null, null);

        eiException eiEx = new eiException(body);
      
    }
}
Best Answer chosen by vinod kumar 794
Prateek Prasoon 25Prateek Prasoon 25

To achieve more test coverage for your eiException class, you need to add test methods to cover the getter and setter methods. Here is an example of how you can test these methods:
@isTest
static void testGettersAndSetters() {
    eiException ex = new eiException();
    ex.code = '123';
    ex.text = 'Test Exception';
    ex.severity = 'Error';
    ex.category = 'General';
    ex.subcategory = 'Unknown';
    ex.dataState = 'Invalid';
    ex.retryAllowed = true;
    ex.additionalInformation = 'Additional info';
    System.assertEquals('123', ex.code);
    System.assertEquals('Test Exception', ex.text);
    System.assertEquals('Error', ex.severity);
    System.assertEquals('General', ex.category);
    System.assertEquals('Unknown', ex.subcategory);
    System.assertEquals('Invalid', ex.dataState);
    System.assertEquals(true, ex.retryAllowed);
    System.assertEquals('Additional info', ex.additionalInformation);
}
This test method creates an instance of the eiException class and sets values for all the properties using the getter and setter methods. Then it uses the System.assertEquals method to verify that the values are set correctly.
Note that you may need to update your eiException constructor to include an empty constructor to initialize the properties before the setter methods are called. You can do this by adding the following code:
public eiException() {
    // Initialize properties
    code = '';
    text = '';
    severity = '';
    category = '';
    subcategory = '';
    dataState = '';
    retryAllowed = false;
    additionalInformation = '';
}

If you find this answer helpful, please mark it as the best answer.
 

All Answers

Prateek Prasoon 25Prateek Prasoon 25

To achieve more test coverage for your eiException class, you need to add test methods to cover the getter and setter methods. Here is an example of how you can test these methods:
@isTest
static void testGettersAndSetters() {
    eiException ex = new eiException();
    ex.code = '123';
    ex.text = 'Test Exception';
    ex.severity = 'Error';
    ex.category = 'General';
    ex.subcategory = 'Unknown';
    ex.dataState = 'Invalid';
    ex.retryAllowed = true;
    ex.additionalInformation = 'Additional info';
    System.assertEquals('123', ex.code);
    System.assertEquals('Test Exception', ex.text);
    System.assertEquals('Error', ex.severity);
    System.assertEquals('General', ex.category);
    System.assertEquals('Unknown', ex.subcategory);
    System.assertEquals('Invalid', ex.dataState);
    System.assertEquals(true, ex.retryAllowed);
    System.assertEquals('Additional info', ex.additionalInformation);
}
This test method creates an instance of the eiException class and sets values for all the properties using the getter and setter methods. Then it uses the System.assertEquals method to verify that the values are set correctly.
Note that you may need to update your eiException constructor to include an empty constructor to initialize the properties before the setter methods are called. You can do this by adding the following code:
public eiException() {
    // Initialize properties
    code = '';
    text = '';
    severity = '';
    category = '';
    subcategory = '';
    dataState = '';
    retryAllowed = false;
    additionalInformation = '';
}

If you find this answer helpful, please mark it as the best answer.
 
This was selected as the best answer
Frank Hart 10Frank Hart 10
Thankful for the little by little useful exercise. Has conclusively the ordinary impact. Flying Together (https://www.flying-together.org/)
Jack Miller 8Jack Miller 8
The code is really great and worked for me Nord VPN 3 year plan (https://sites.google.com/view/nord-vpn-3-year-plan/) . 
hatch hozzenhatch hozzen
Self-Awareness: Iqbal emphasized the importance of self-awareness. He believed that individuals must introspect and gain insight into their own inner selves, including their thoughts, emotions, and desires.
Unity of Self: Iqbal argued that the self is not a fragmented entity but a unified whole. To achieve self-realization, one must harmonize the various aspects of their personality and bring them into alignment with a   https://www.dillonhailrepair.com/ higher purpose.
Spiritual Awakening: Iqbal's philosophy places great importance on spirituality. He believed that self-realization involves a spiritual awakening, where individuals connect with a higher reality or the Divine. This connection leads to a sense of purpose and direction in life.
Struggle and Effort: Iqbal emphasized that self-realization is not an easy task. It requires effort, self-discipline, and a continuous struggle to overcome weaknesses and limitations.
Social Responsibility: Iqbal believed that once individuals achieve self-realization, they should use their enhanced abilities and understanding to serve society. He called for individuals to become "Khudi" (selfhood) and then work for the betterment of the community.