+ Start a Discussion

Tests class

Hi Friends,


I need to write test class for the below given class.Can somebody tell me how to go about it.?

I know I have to use @Tests keyword.But I am looking for more than this.


public class hermi
public User u;
public Contact c;
public hermi()
User u;
Contact c;
u=[select username,contactid from user where id='005c0000000Et6I'];

c=[select name,email,firstname,lastname from contact where id=:u.contactid];
//System.debug('The value in the user object 'u' is'+u.username);
System.debug('The value in the user object is'+u.contactid);

System.debug('The value in the contact object of type contact is'+c);
System.debug('The value in the variable of contact type issajkdskjdkjdksadksad'+c.email);
//System.debug('The value in the variable of type issajkdskjdkjdksadksad'+c.firstname);
//System.debug('The value in the variable of type issajkdskjdkjdksadksad'+c.lastname);
public contact getcontact()
return c;
public User getuser()
return u;







Below is the test class and method that are enough for you to cover the code.


public class testhermi

 static testmethod void testhermii()
hermi he = new hermi();

 But I have below suggestions for you regarding your class and code.

Your Code should not contain the Hardcoded ids of anything. It is not a best practice.

You should get any id dynamically by querying or any other way.

In the 9th line you put a hardcoded id which is not good.

And whenever a list returns something before using  the content of the list you should check whether the list has really some data like list.size()>0 like that.

So that it will not give test failure errors.


Test class that I gave will lead to test failures when that particular use have no contacts.

So please modify the main class code accordingly and then use/write the given test method.

While writing the test class provide enough data to test method so that it can cover the all code blocks.