+ 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/)