[SalesForce] apex test public void – Non static method cannot be referenced from a static context

I have an apex class named votingRelationHandler. It is called from a trigger and works great but I am having issues with writing the test class.

Issue in the Test Class:

Non static method cannot be referenced from a static context: void votingRelationHandler.company_voting_member()

In addition I think it may also later complain about sending in values to represent the trigger.old, trigger.oldMap, trigger.new and trigger.newMap.

Is this possible to capture / emulate from the record used in test setup?

TRIGGER:

trigger votingRelation on Voting_Relation__c (before insert , after insert, before update, after update)
{
    votingRelationHandler handle    =   new votingRelationHandler(trigger.old, trigger.oldMap , trigger.new , trigger.newMap);

    if (trigger.isBefore)
    {
        if (trigger.isInsert) handle.beforeInsert();
        if (trigger.isUpdate) handle.beforeUpdate();
    }
    if (trigger.isAfter)
    {
        if (trigger.isInsert) handle.afterInsert();
        if (trigger.isUpdate) handle.afterUpdate();
    }

} // end trigger

MAIN CLASS:

public class votingRelationHandler
{
    final   List<Voting_Relation__c>                oldRecords;
    final   List<Voting_Relation__c>                newRecords;
    final   Map<Id, Voting_Relation__c>             oldMap;
    final   Map<Id, Voting_Relation__c>             newMap;

    public votingRelationHandler(List<Voting_Relation__c> oldRecords, Map<ID, Voting_Relation__c> oldMap , List<Voting_Relation__c> newRecords, Map<Id, Voting_Relation__c> newMap)
    {
        this.oldRecords                         =   oldRecords;
        this.newRecords                         =   newRecords;
        this.oldMap                             =   oldMap;
        this.newMap                             =   newMap;
    }

    public void afterInsert()
    {
        company_voting_member();
    }

    public void company_voting_member ()
    {
        for(Voting_Relation__c rec_VR : newRecords)
        {
           // update account record
        }
    }

} // end class

TEST CLASS:

@isTest
private class votingRelationHandlerTest
{
    @testSetup static void setup()
    {
        //ACCOUNTS
        Account Account_VotingMember                =   TestDataFactory.createAccount_VotingMember();
        Insert  Account_VotingMember;

        Account Account_AffiliateMember             =   TestDataFactory.createAccount_AffiliateMember();
        Insert  Account_AffiliateMember;

        //VOTING RELATION
        voting_relation__c VR_A                     =   New voting_relation__c
            (
                voting_member__c                    =   Account_VotingMember.Id,
                company__c                          =   Account_AffiliateMember.id,
                Effective_Date__c                   =   Date.today(),
                Terminate_Date__c                   =   NULL

            );
        insert VR_A;

    } // end setup

    static testMethod void Test()
    {
        //How can I pass VR_A data as trigger.old, trigger.oldMap, trigger.new , trigger.newMap

        //ACCOUNTS
        Account acc_voting                          =   [SELECT Id
                                                     FROM Account
                                                     WHERE Name     =   :PJM_GLOBAL.account_voting_member
                                                    ];

        Account acc_affiliate                       =   [SELECT Id
                                                     FROM Account
                                                     WHERE Name     =   :PJM_GLOBAL.account_affiliate_member
                                                    ];
        //VOTING RELATION (VOTING FAMILY)
        voting_relation__c VR_1                     =   [SELECT Id
                                                     FROM   voting_relation__c
                                                     WHERE  voting_member__c                =   :acc_voting.id
                                                     AND    company__c                      =   :acc_affiliate.Id
                                                     AND    Effective_Date__c               =   :PJM_GLOBAL.date_today
                                                     AND    Terminate_Date__c               =   NULL
                                                    ];


        votingRelationHandler.company_voting_member();  <---- ISSUE IS HERE
    }

} // end test class

Best Answer

When methods are not marked static, you have to first instantiate the class. In this case, you have to write it like this:

votingRelationHandler handler = new votingRelationHandler(<pass your records here>);
handler.company_voting_member();

UPDATE: I see you updated your original code and my answer is no longer answering your question.

Your original constructor has four parameters, and you're passing only one. Make sure pass null for any parameters you don't want your controller to handle.

This are your four established parameters:

new votingRelationHandler(oldRecords, oldMap, newRecords, newMap);
Related Topic