You need to sign in to do that
Don't have an account?

how can i write test class for my controller
Hai guys.
When i am writng the the testclass for following controller it is displaying the error.
controller
-----------------
public class Action_Function_Controller
{
public Account acobj{get;set;}
pagereference pag = null;
public Action_Function_Controller()
{
acobj = new Account();
}
public pagereference save()
{
// creating if condidtion
if(acobj.name != '')
{
insert acobj;
}
else
{
//creating a error message
ApexPages.Message msg = new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Error:Enter Name');
ApexPages.addMessage(msg);
}
//Creting another if condition
if(acobj.id != null)
{
// Send the user to the detail page for the new account.
pag = new pagereference('/'+acobj.id);
pag.setredirect(true);
}
return pag;
}
}
testclass
-------------------------
@isTest
public class ActionFunctionControllerTest
{
public static testmethod void testfunction()
{
//inserting record into the account object
Account acc = new Account();
acc.name = 'Test Account';
acc.industry = 'Accounts';
insert acc;
//using the page reference
pagereference pg = page.Action_Function_VF_PAge;
test.setcurrentpage(pg);
//calling controller class
Action_Function_Controller afc = new Action_Function_Controller();
//calling the variables into testclass
//afc.acobj;
if(acc.name !='')
{
insert acc;
}
afc.save();
}
}
when i run the test class following are occured
System.DmlException: Insert failed. First exception on row 0 with id 00128000008E4KRAA0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Please help me any one.
When i am writng the the testclass for following controller it is displaying the error.
controller
-----------------
public class Action_Function_Controller
{
public Account acobj{get;set;}
pagereference pag = null;
public Action_Function_Controller()
{
acobj = new Account();
}
public pagereference save()
{
// creating if condidtion
if(acobj.name != '')
{
insert acobj;
}
else
{
//creating a error message
ApexPages.Message msg = new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Error:Enter Name');
ApexPages.addMessage(msg);
}
//Creting another if condition
if(acobj.id != null)
{
// Send the user to the detail page for the new account.
pag = new pagereference('/'+acobj.id);
pag.setredirect(true);
}
return pag;
}
}
testclass
-------------------------
@isTest
public class ActionFunctionControllerTest
{
public static testmethod void testfunction()
{
//inserting record into the account object
Account acc = new Account();
acc.name = 'Test Account';
acc.industry = 'Accounts';
insert acc;
//using the page reference
pagereference pg = page.Action_Function_VF_PAge;
test.setcurrentpage(pg);
//calling controller class
Action_Function_Controller afc = new Action_Function_Controller();
//calling the variables into testclass
//afc.acobj;
if(acc.name !='')
{
insert acc;
}
afc.save();
}
}
when i run the test class following are occured
System.DmlException: Insert failed. First exception on row 0 with id 00128000008E4KRAA0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Please help me any one.
Problem is that you can try to insert record with id.
There u insert account
and here you ansert account too
Maybe it's help write upsert instead of insert.
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks,
Alex