0

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();
    }

}
2
  • Is that controller extension for Contact or RelatieContact__c? cause in extension it is contact, and in unit test RelatieContact__c id is passed
    – kurunve
    Oct 24, 2017 at 7:21
  • @kurunve I dont know if I properly understood your question. My class works fine the error is generated when I run my testclass.
    – Jenssen
    Oct 24, 2017 at 8:22

1 Answer 1

2

This error is coming because your standard controller expect type of Contact while you are passing Relatie_Contact__c in your test class

ApexPages.StandardController sc = new ApexPages.standardController(rctest);

So you need to change this into

ApexPages.StandardController sc = new ApexPages.standardController(cont);
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .