2

I created a class the will return set and get the parameter to rest service here is the class

public class LeadUtils

{  
 public String First_nm;

 public String Last_nm;

 public String Phone;

 public String Email_id;

 public String Company;

           public String getFirstName() {
                  return this.First_nm;
            }

            public void setFirstName(String First_nm) {
                  this.First_nm= First_nm;
            }

            public String getLastName() {
                  return this.Last_nm;
            }

            public void setLastName(String Last_nm) {
                  this.Last_nm= Last_nm;
            }
            public String getPhone() {
                  return this.Phone;
            }

            public void setPhone(String Phone) {
                  this.Phone= Phone;
            }
            public String getEmail() {
                  return this.Email_id;
            }

            public void setEmail(String Email_id) {
                  this.Email_id= Email_id;
             }     
            public String getCompany() {
                  return this.Company;
            }

            public void setCompany(String Company) {
                  this.Company= Company;
            }


 }

And create test class for this

@isTest

public class LeadUtilsTest

{

    public static testMethod void testParameter() {

        // Instantiate a new controller with all parameters in the page

        LeadUtils Utils= new LeadUtils();

        Utils.setFirstName('lastname');

        Utils.setLastName('firstname');

        Utils.setPhone('[email protected]');

        Utils.setEmail('[email protected]');

        Utils.setCompany('acme');

        // Verify that the success page displays

        Lead[] leads = [select id, email from lead where Company = 'VVDN'];

        //System.assertEquals('[email protected]', leads[0].email);
    }
}

When i run the test it shows 60% code convergence. But I need above 75% for the deployment

1 Answer 1

4

You haven't covered your getters in your test method. Same as the setters you need to call the getters at least once. In your test method after Utils.setCompany('acme'); add the following:

String firstName = Utils.getFirstName();
String lastName = Utils.getLastName();
String phone = Utils.getPhone();
String email = Utils.getEmail();
String company = Utils.getCompany();
2
  • 2
    I know this example is extremely basic, but I always try to never call a method in testing without some sort of assertion. You shouldn't test for code coverage, but for logical branch coverage. I would just add a few lines, where you set the variable, call the getter, and then assert the value is equal to what you set it to. I know in this example, it might be overkill, but it's a good habit to get into. Assertions, assertions, assertions.... :) Commented Aug 13, 2014 at 16:32
  • 100% agree, it's not just for the sake of getting 100% coverage, assertions are crucial to ensure the code is behaving properly. This answer was just purely to show what's missing, leaving the assertions on @user10843 :) Commented Aug 13, 2014 at 23:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .