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

Test method help
I'm new to Apex/Visforce and I'm having trouble wrapping my brain around test methods and getting them to work. I currently have a method that is giving me the following error and I can't figure out why:
System.SObjectException: Field is not writeable: Merchant_Analysis__c.Opportunity__c
It refers to the bolded line below:
public PageReference save()
{
analysis.Opportunity__c = oppID;
analysis.recordtypeid = recType;
insert analysis;
PageReference analysisPage = new PageReference('/' + analysis.id);
analysisPage.setRedirect(true);
return analysisPage;
}
static testMethod void testSave()
{
analysisController controller = new analysisController();
String recType = '012400000005QI0AAM';
Opportunity o = new Opportunity(name='test opp',stagename='test stage',closedate=System.today());
insert o;
Merchant_Analysis__c a = new Merchant_Analysis__c();
a.Opportunity__c = o.id;
a.recordtypeid=recType;
insert a;
PageReference testPage = new PageReference('/' + a.id);
testPage.setRedirect(true);
controller.oppID = o.id;
controller.recType = recType;
controller.analysis = a;
String ref0 = testPage.getUrl();
String ref1 = controller.save().getUrl();
System.assertEquals(ref0,ref1);
}
If someone could help troubleshoot this issue with me that would be awesome.
Thanks,
Jonathan
oppID is a public class level variable that simply holds the ID of the opportunity that we're attaching this custom object too. I use the following to set oppID in the class constructor:
System.currentPageReference().getParameters().get('id');