You need to sign in to do that
Don't have an account?
資訊部
How to write this test class?
Hi guy ,
this is my practice class and test class.I don't understand why my test class can not pass. Can someone help me? please...
this is my practice class and test class.I don't understand why my test class can not pass. Can someone help me? please...
public with sharing class ContactExtension { public Contact cont{get;set;} public String Phone{get;set;} public ContactExtension(ApexPages.StandardController controller){ Id ContactId = ApexPages.currentPage().getParameters().get('Id'); this.cont = (Contact)controller.getrecord(); Contact cont = new Contact(); } public PageReference save(){ PageReference pagereference; if(cont.Phone != null){ Phone = cont.phone; cont.Phone = '886'+cont.Phone; cont.MobilePhone = '886'+ Phone; insert cont; pagereference = new PageReference('/'+cont.id); } return pagereference; } }
@isTest public class testContactExtension { static testMethod void testm1(){ test.startTest(); Contact tcontact = new Contact(LastName='TEST',Phone='0973768421',Email='Test@jzn.com.tw',JOB_STATUS__c ='在職'); insert tcontact; PageReference pageRef=new PageReference ('/apex/PageTryInput'); test.setCurrentPage(pageRef); pageRef.getParameters().put('Id',String.valueOf(tcontact.Id)); ApexPages.StandardController thecontroller; ContactExtension cls = new ContactExtension(thecontroller); cls.Phone ='00000'; cls.cont = new Contact(); cls.cont.Phone = '0000000'; cls.cont.MobilePhone = cls.Phone + cls.cont.Phone; cls.save(); test.stopTest(); } }
Try the following test class, it may be helpful for you:
@isTest
public class PageReference_Test
{
@isTest
static testMethod void testMethod(){
Contact con =new Contact();
con.LastName='Test';
con.Phone=987654321;
insert con;
Test.StartTest();
ApexPages.StandardController sc = new ApexPages.StandardController(con);
ContactExtension testAccPlan = new ContactExtension(sc);
PageReference pageRef = Page.PageName; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(con.Id));
Test.setCurrentPage(pageRef);
testAccPlan.save(); call all your function here
Test.StopTest();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi