+ Start a Discussion
SS KarthickSS Karthick 

Regarding Testing

Hi folks,
   Can anyone tell me how to test the following code:

public class CreateAccount{

Account[]  a=new Account[3]
a[0]=new account (Name=Account1);
a[1]=new account (Name=Account2);
a[2]=new account (Name=Account3);
try{
Database.Insert(a,false);
}
catch(Exception e){
system.debug('Error Message:' +e.getMessage());
}

}


Thanks in advance
Karthick
Best Answer chosen by SS Karthick
ShashForceShashForce
With your trigger, what you are doing is create a new contact whenever an account record is inserted. So, all you need to do in your test script is to create an account.

@istest
public class accounttest {
	static testmethod void insertaccount(){
		account ac = new account(name='testacc');
		
		test.starttest();
		
		insert ac;
		list<contact> clist = [select accountId, lastname from contact where accountId=:ac.Id];
		for(contact ct:clist){
			system.assertequals(lastname.substring(0,8),'lastname');
		}
		
		test.stoptest();
	}
}

I would also suggest you to go through this: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank

All Answers

ShashForceShashForce
You might want to include your code in a method within the class which can be called by a trigger or a visualforce page. You can then write a test class for it by writing a script to invoke the class's method.
SS KarthickSS Karthick

Hy Shashank_SFDC,

Lets consider this:

Trigger CreateContact Account (before insert){
Contact[] c=new contact[3];
  for(Account a:trigger.new){

     c[0]=new Contact(Account=a,LastName=lastname1);
c[1]=new Contact(Account=a,LastName=lastname2);
c[2]=new Contact(Account=a,LastName=lastname3);
}
try{
Database.Insert(c,false);
}
catch(Exception e){
system.debug('Error Message:' +e.getMessage());
}

}

 

For This how to write Positive Test and Negative Test???

Please Help!

Sonam_SFDCSonam_SFDC
As you have written a class which you wish to test, you can include 
________
Account[]  a=new Account[3]
a[0]=new account (Name=Account1);
a[1]=new account (Name=Account2);
a[2]=new account (Name=Account3);
try{
Database.Insert(a,false);
}
catch(Exception e){
system.debug('Error Message:' +e.getMessage());

_________
in a method inside the class  and call the function in a test class

something similar to :
http://salesforce.stackexchange.com/questions/36680/how-to-write-test-class-for-below-apex-class
ShashForceShashForce
With your trigger, what you are doing is create a new contact whenever an account record is inserted. So, all you need to do in your test script is to create an account.

@istest
public class accounttest {
	static testmethod void insertaccount(){
		account ac = new account(name='testacc');
		
		test.starttest();
		
		insert ac;
		list<contact> clist = [select accountId, lastname from contact where accountId=:ac.Id];
		for(contact ct:clist){
			system.assertequals(lastname.substring(0,8),'lastname');
		}
		
		test.stoptest();
	}
}

I would also suggest you to go through this: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
This was selected as the best answer