function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ioannis Sideris 6Ioannis Sideris 6 

Need help on writing a Unit Test

Hello everyone, 

I am a fresh new developer and recently I wrote my first method in a class of an existing project. I need to write also a unit test for this class but I am getting an error and since I just recently started writind code in general I am struggling to deal with the issue. Any help would be much appreciated. 

Below is my method: 

// update the accounts of Type 'Other'PEM with the PEM of their related 'Constituent' Account
    public static void updateRelatedAccountPEM(Set<String> setAccountId) {
    // query all relevant relationships, where the related person is account type 'Other' and person is 'Constituent'
         Map<Id,Account> accountMap = new Map<Id,Account>();
            for (Relationship__c relationship : [
                SELECT Id, Person__c, Related_Person__c, Person__r.OwnerId, Related_Person__r.OwnerId
                FROM Relationship__c
                WHERE Person__r.RecordType.DeveloperName = :Utils_Constants.ACCOUNT_CONSTITUENT_RT
                AND Related_Person__r.RecordType.DeveloperName = :Utils_Constants.ACCOUNT_OTHER_RT
                AND (Person__c IN :setAccountId
                OR Related_Person__c IN :setAccountId)
            ]) {
                if(relationship.Person__r.OwnerId != relationship.Related_Person__r.OwnerId) {
                   accountMap.put(
                    relationship.Related_Person__c,
                    new Account(Id = relationship.Related_Person__c, OwnerId = relationship.Person__r.OwnerID)
                    );
                }
            }
            if (!accountMap.isEmpty()) {
                Database.SaveResult[] result = Database.update(accountMap.values(), false);
                Utils_Methods.writeErrorLog(
                    accountMap.values(),
                    result,
                    'AccountService.updateRelatedAccountPEM'
                );
            }
     }

For Data Creation I use the following method: 

global static final TestDataFactory.Template PERSONAL_RELATIONSHIP = new TestDataFactory.Template(
        Relationship__c.SObjectType,
        new Map<SObjectField, Object>{
            Relationship__c.Name => 'testRelationship {!index}',
            Relationship__c.Person__c => AccountTestDataTemplates.CONSTITUENT,
            Relationship__c.Related_Person__c => AccountTestDataTemplates.OTHER,
            Relationship__c.RecordTypeId => Utils_Constants.RELATIONSHIP_PERSONAL_RT
        }
    );

My Unit Test is:

@IsTest
public class updatePEMTest {
    @IsTest
    public static void testUpdateRelatedAccountPEM() {
        // Create test data using TestDataFactory
        TestDataFactory factory = new TestDataFactory();
        List<Relationship__c> relationships = (List<Relationship__c>)factory.create(AccountTestDataTemplates.PERSONAL_RELATIONSHIP);
        
               
        // Call the method being tested
        Set<String> setAccountId = new Set<String>();
        for (Relationship__c relationship : relationships) {
            setAccountId.add(relationship.Person__c);
            setAccountId.add(relationship.Related_Person__c);
        }
        
        Test.startTest();
        AccountService.updateRelatedAccountPEM(setAccountId);
        Test.stopTest();
        
        // Verify the results
        Map<Id, Account> updatedAccounts = new Map<Id, Account>([SELECT Id, OwnerId FROM Account WHERE Id IN :setAccountId]);
        for (Relationship__c relationship : relationships) {
            Account relatedAccount = updatedAccounts.get(relationship.Related_Person__c);
            System.assertEquals(relationship.Person__r.OwnerId, relationship.Related_Person__r.OwnerId);
        }
    }
}


The error I receive on the Unit Test is: 

Incompatible types since an instance of SObject is never an instance of List<Relationship__c>

Any feedback/help would be much appreciated! 

Lexi KansLexi Kans
Create a test environment that closely mimics the conditions in which the unit will be used. This may involve creating test data, setting up dependencies, or mocking/stubbing external services or components. Determine the expected output or behavior of the unit under test. This involves understanding the requirements and specifications for the function or module being tested. Compose the test case, which consists of one or more test methods that exercise the unit under test and validate its behavior. Each zeeto (https://zeeto.pk/) test method should focus on a specific aspect or scenario. If the unit under test requires specific preconditions, such as the existence of certain data or the configuration of certain objects, set them up before executing the test method.