+ Start a Discussion
MaddyConnectMaddyConnect 

Test coverage for class properties using test class

Hi,

I have below class defination and it has no method:

public class ExampleClass
{
        public String name {get; set;}
        public String Week_Ending{get; set;}
        public String Depot {get; set;}
        public String Type_Of_Work{get; set;}
}

I tried various ways to cover this class with no luck. Please help to resolve test class to cover such type of class.

Thanks,
Maddy
Best Answer chosen by MaddyConnect
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Maddy,
Use Below Code 
public class textData
{
         public String name {get; set;}
         public String Week_Ending{get; set;}
         public String Project_Ref{get; set;}
         public String Project_Ref_Name{get; set;}
         public String Depot{get; set;}
         public String Type_Of_Work{get; set;}
         public String Lay_Material{get; set;}
         public String Material_Type{get; set;}
         public String Diameter_Laid{get; set;}
         public String Abandon_Main_Length{get; set;}
         public String Abandon_Main_Size_Imperial{get; set;}
         public String Abandon_Main_Size_metric{get; set;}
         public String DIV_LT180mm{get; set;}
         public String DIV_GT250mm{get; set;}
         public String Reinf_LT180mm{get; set;}
         public String Reinf_GT250mm{get; set;}
         public String Repl_LT180mm_INS{get; set;}
         public String Repl_LT180mm_Mole{get; set;}
         public String Repl_LT180_O_C{get; set;}
         public String Repl_GT180_LT355_Mole{get; set;}
         public String Repl_GT180_LT355INS{get; set;}
         public String Repl_GT180_LT355O_C{get; set;}
         public String Repl_GT355mm_INS{get; set;}
         public String Repl_GT355mm_Mole{get; set;}
         public String Repl_GT355mm_O_C{get; set;}
         public String Services_Non_Dom{get; set;}
         public String Services_Relay_INS{get; set;}
         public String Services_Relay_Mole{get; set;}
        public String Services_Relay_O_C{get; set;}
        public String Services_Transfer{get; set;}
        public String Team{get; set;}
        public textData(){}
        
    }

Test Class:
@isTest
private class textDataTest 
{
    static testMethod void myUnitTest() 
    {
        textData t = new textData();
        //Tried by assigning values as below
        t.Depot = 'test';
        t.Abandon_Main_Length = 'test';
        t.Abandon_Main_Size_Imperial = 'test';
        t.Abandon_Main_Size_metric = 'test';
        t.Diameter_Laid = 'test';
        t.DIV_GT250mm = 'test';
        t.DIV_LT180mm = 'test'; 
    }
}

Let us know if it helps you.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

Just create an instance of this class like below line. it will automatically be covered then.
ExampleClass exampleInstance = new ExampleClass();

Let us know if it helps you.
 
Arunkumar RArunkumar R
Hi Maddy,

Just initializing your apex class in test method is sufficient for your problem,
 
@isTest
private class TestExampleClass
{

static testMethod void initVariables()
{
ExampleClass ex = new ExampleClass();
}

}

Please go through the below link for how to write test class, 
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Please mark this as a solution if you satisfied..!
MaddyConnectMaddyConnect
Ashish, Arunkumar,

I already tried creating instance of class to be tested in test class. Its not covering single line. Thanks for your replies.

Thanks,
Maddy
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Maddy,

Please paste your test class here.
 
sandeep sankhlasandeep sankhla
Hi Maddy,

You just create a constructor and initalize all your variables..Because as a best practsie we should always initalize all are varaiables..

like
 
public class ExampleClass
{
        public String name {get; set;}
        public String Week_Ending{get; set;}
        public String Depot {get; set;}
        public String Type_Of_Work{get; set;}


        public ExampleClass()
        {
            name = Week_Ending = Depot  = Type_Of_Work = '';
         }
}

Then you cna simply create instance of this class it will cover..make sure you are createing instance in test class then only it will cover...not in normal apex class...

your test class will have test method where you will cerate instance
 
static testMethod void myUnitTest() {




TestExampleClass obj = new TestExampleClass ();


}


P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
MaddyConnectMaddyConnect
Class File:

public class textData
{
         public String name {get; set;}
         public String Week_Ending{get; set;}
         public String Project_Ref{get; set;}
         public String Project_Ref_Name{get; set;}
         public String Depot{get; set;}
         public String Type_Of_Work{get; set;}
         public String Lay_Material{get; set;}
         public String Material_Type{get; set;}
         public String Diameter_Laid{get; set;}
         public String Abandon_Main_Length{get; set;}
         public String Abandon_Main_Size_Imperial{get; set;}
         public String Abandon_Main_Size_metric{get; set;}
         public String DIV_LT180mm{get; set;}
         public String DIV_GT250mm{get; set;}
         public String Reinf_LT180mm{get; set;}
         public String Reinf_GT250mm{get; set;}
         public String Repl_LT180mm_INS{get; set;}
         public String Repl_LT180mm_Mole{get; set;}
         public String Repl_LT180_O_C{get; set;}
         public String Repl_GT180_LT355_Mole{get; set;}
         public String Repl_GT180_LT355INS{get; set;}
         public String Repl_GT180_LT355O_C{get; set;}
         public String Repl_GT355mm_INS{get; set;}
         public String Repl_GT355mm_Mole{get; set;}
         public String Repl_GT355mm_O_C{get; set;}
         public String Services_Non_Dom{get; set;}
         public String Services_Relay_INS{get; set;}
         public String Services_Relay_Mole{get; set;}
        public String Services_Relay_O_C{get; set;}
        public String Services_Transfer{get; set;}
        public String Team{get; set;}
    }

Test Class: 

@isTest
private class textDataTest 
{
    static testMethod void myUnitTest() 
    {
        textData t = new textData();
        //Tried by assigning values as below
        /*t.Depot = 'test';
        t.Abandon_Main_Length = 'test';
        t.Abandon_Main_Size_Imperial = 'test';
        t.Abandon_Main_Size_metric = 'test';
        t.Diameter_Laid = 'test';
        t.DIV_GT250mm = 'test';
        t.DIV_LT180mm = 'test'; */ 
    }
}
ManojjenaManojjena
Hi Maddy,

Please change your class code with below then try to write test class 
public class ExampleClass
{
        public String name {get; set;}
        public String Week_Ending{get; set;}
        public String Depot {get; set;}
        public String Type_Of_Work{get; set;}
		public ExampleClass(){
		
		}
}
 
@isTest
public class Test  ExampleClass{
	public  static testMethod void  unitTest(){
	  ExampleClass exm=new ExampleClass();
	}
}

I think it will solve your problem .

 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Maddy,
Use Below Code 
public class textData
{
         public String name {get; set;}
         public String Week_Ending{get; set;}
         public String Project_Ref{get; set;}
         public String Project_Ref_Name{get; set;}
         public String Depot{get; set;}
         public String Type_Of_Work{get; set;}
         public String Lay_Material{get; set;}
         public String Material_Type{get; set;}
         public String Diameter_Laid{get; set;}
         public String Abandon_Main_Length{get; set;}
         public String Abandon_Main_Size_Imperial{get; set;}
         public String Abandon_Main_Size_metric{get; set;}
         public String DIV_LT180mm{get; set;}
         public String DIV_GT250mm{get; set;}
         public String Reinf_LT180mm{get; set;}
         public String Reinf_GT250mm{get; set;}
         public String Repl_LT180mm_INS{get; set;}
         public String Repl_LT180mm_Mole{get; set;}
         public String Repl_LT180_O_C{get; set;}
         public String Repl_GT180_LT355_Mole{get; set;}
         public String Repl_GT180_LT355INS{get; set;}
         public String Repl_GT180_LT355O_C{get; set;}
         public String Repl_GT355mm_INS{get; set;}
         public String Repl_GT355mm_Mole{get; set;}
         public String Repl_GT355mm_O_C{get; set;}
         public String Services_Non_Dom{get; set;}
         public String Services_Relay_INS{get; set;}
         public String Services_Relay_Mole{get; set;}
        public String Services_Relay_O_C{get; set;}
        public String Services_Transfer{get; set;}
        public String Team{get; set;}
        public textData(){}
        
    }

Test Class:
@isTest
private class textDataTest 
{
    static testMethod void myUnitTest() 
    {
        textData t = new textData();
        //Tried by assigning values as below
        t.Depot = 'test';
        t.Abandon_Main_Length = 'test';
        t.Abandon_Main_Size_Imperial = 'test';
        t.Abandon_Main_Size_metric = 'test';
        t.Diameter_Laid = 'test';
        t.DIV_GT250mm = 'test';
        t.DIV_LT180mm = 'test'; 
    }
}

Let us know if it helps you.
This was selected as the best answer
MaddyConnectMaddyConnect
Thanks guys for all your prompt replies. The issue is resolved when I run test class in 'Developer Console' instead of 'Force.com IDE' with no change in main class with below test class:

@isTest
private class textDataTest 
{
    static testMethod void myUnitTest() 
    {
        textData t = new textData();
        t.Depot = 'test';
        t.Abandon_Main_Length = 'test';
        t.Abandon_Main_Size_Imperial = 'test';
        //Assign values to all memebers of class
    }
}
sathish t 38sathish t 38
this code 0% coverage ....but wrote the test class crctly...any help 

apex class:
public class JiraDataCommon {
    public class JiraUser{
        public String name;
        public String emailAddress;
        public String accountId;
        public String displayName;
    }

    public class JiraSearchRequest{
        public List<String> expand;
        public String jql;
        public Integer maxResults;
        public Boolean fieldsByKeys;
        public List<String> fields;
        public Integer startAt;
    }
    public class JiraNotificationRequest{
        public String htmlBody;
        public String subject;
        public String textBody;
        public Map<String,Object> to;
    }
}

Apex test class:
@isTest
public class JiraDataCommon_Test{

   static testMethod void test_JiraUser (){
    JiraDataCommon.JiraUser obj11 = new JiraDataCommon.JiraUser();
    obj11.name = 'test1';
    obj11.emailAddress = 'sfadmin@demandblue.com';
    obj11.accountId = '01p2v00000INJ32';
    obj11.displayName = 'test data'; 
  }
      static testMethod void test_JiraSearchRequest(){
   JiraDataCommon.JiraSearchRequest obj61 = new JiraDataCommon.JiraSearchRequest();
    obj61.expand = new List<String>();
    obj61.jql = 'test data';
    obj61.maxResults = 10;
    obj61.fieldsByKeys = false;
    obj61.fields = new List<String>();
    obj61.startAt = 10;
  }
  static testMethod void test_JiraNotificationRequest(){
  JiraDataCommon.JiraNotificationRequest obj131 = new JiraDataCommon.JiraNotificationRequest();
    obj131.htmlBody = 'htmlbody';
    obj131.subject = 'subject';
    obj131.textBody = 'textbody';
    obj131.to = new Map<String,Object>();
  }

}