I get the error:Invalid conversion from runtime type Relatie_Contact__c to Contact for my testclass. I have looked trough some similar questions but could find the answer for my case I have the following:
Class:
public class NieuweRelatieContactRec {
public Contact c {get;set;}
Public Relatie_Contact__c Rc = new Relatie_Contact__c();
public NieuweRelatieContactRec(ApexPages.StandardController controller) {
this.c = (Contact)controller.getRecord();
}
public PageReference NieuweRelatieContact() {
Rc.Relatiesoort__c= c.Relatiesoort__c;
Rc.Relatietype__c='Tennisleraar';
Rc.Contactpersoon__c= c.Id;
Rc.Organisatie__c = c.AccountId;
insert rc;
return new PageReference('javascript:window.close()');
}
}
Test: @isTest
private class NieuweRelatieContactRecTestClass
{
Public Relatie_Contact__c Rctest = new Relatie_Contact__c();
private static testMethod void testNieuweRelatieContact() {
test.startTest();
PageReference pageRef = Page.NieuweRelatie;
Account acc = new Account(Name='Abce');
insert acc;
Contact cont= new Contact(Lastname='Lastnametest123',AccountId=acc.Id);
Insert cont;
Relatie_Contact__c Rctest = new Relatie_Contact__c(Relatiesoort__c='Vereniging: Medewerker',
Relatietype__c='Tennisleraar',
Contactpersoon__c= cont.Id,
Organisatie__c = cont.AccountId);
insert rctest;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id',rctest.id);
ApexPages.StandardController sc = new ApexPages.standardController(rctest);
NieuweRelatieContactRec controller = new NieuweRelatieContactRec(sc);
System.assertNotEquals(null,controller.NieuweRelatieContact());
test.stopTest();
}
}