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
Jose Boveda 6Jose Boveda 6 

Assertion failing for Trigger test class even though data looks correct

Hi folks,
Found something interesting while researching regarding another posted bug:
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BYt2

I've got a trigger and a test class for it. The trigger basically takes a related list of values and concatenates it, saving it as part of the Account object. My test cases all fail, showing the following error:
Class.TMCR_testAccountInterestList.test_addInterest: line 51, column 1
14:18:05.697 (6697124228)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: foo
Interest0
Interest1
Interest2
, Actual: foo
Interest0
Interest1
Interest2

The data looks identical. Just to be sure, I put the strings through a hex conversion to compare their hex values and they are indeed different.

My question is what gives? Why are they different? The data going in should be the same.

For reference, here are is the Trigger:
trigger Account_UpdateAccountInterestsList on Interest__c (after delete, after insert, after undelete, 
after update) {
	Interest__c[] interests;
    if (Trigger.isDelete) 
        interests = Trigger.old;
    else
        interests = Trigger.new;
	Set<ID> affected_accounts = new Set<ID>();
	Map<ID, Set<String>> account_interests = new Map<ID, Set<String>>();
	for(Interest__c interest: interests){
		affected_accounts.add(interest.Account__c);
		account_interests.put(interest.Account__c, new Set<String>());
	}

	List<Interest__c> all_interests = [
		SELECT Id, Account__c, Interest__r.Name
		FROM Interest__c
		WHERE Account__c IN :affected_accounts
		ORDER BY Interest__r.Name
	];
	List<Account> accounts_to_update = [
		SELECT Id, Account_Interests_List__c
		FROM Account
		WHERE Id IN :affected_accounts
	];
	
	for(Interest__c interest: all_interests){
		account_interests.get(interest.Account__c).add(interest.Interest__r.Name);
	}
	for(Account account: accounts_to_update){
		account.Account_Interests_List__c = Util_TMCR.combineList(account_interests.get(account.Id));
	}
	
	update accounts_to_update;
}
Here is the test class:
@isTest
private class TMCR_testAccountInterestList {
	static Account getAccount(){
		Util_TMCR_DataFactory.accounts account = new Util_TMCR_DataFactory.accounts();
		Account acc = [
			SELECT Id, Account_Interests_List__c
			FROM Account WHERE Name = :account.get('name')
		];
		return acc;
	}
	static Interests__c addInterest(String name){
		Interests__c new_interest = new Interests__c(Name = name);
		insert new_interest;
		return new_interest;
	}
	static Interest__c addToAccount(Account acc, Interests__c interest){
		Interest__c acc_interest = new Interest__c(Interest__c = interest.Id, Account__c = acc.Id);
		insert acc_interest;
		return acc_interest;
	}
	
	@testSetup
	static void setup(){
		List<Interests__c> interests = new List<Interests__c>();
		List<Interest__c> account_interests = new List<Interest__c>();
		for(Integer i=0; i<3;i++){
			interests.add(addInterest('Interest' + i));
		}
		Util_TMCR_DataFactory.accounts account = new Util_TMCR_DataFactory.accounts();
		Account acc = account.generate();
		for(Interests__c interest: interests){
			account_interests.add(addToAccount(acc, interest));
		}
	}
	
    static testMethod void test_addInterest() {
		test.startTest();
		Account account = getAccount();
		Interests__c interest = addInterest('foo');
		Interest__c acc_interest = addToAccount(account, interest);
		test.stopTest();
		
		String concat = Util_TMCR.combineList(new List<String>{'foo', 'Interest0', 'Interest1', 'Interest2'});
		Account acc = getAccount();
		System.assertEquals(concat, acc.Account_Interests_List__c);
    }
    static testMethod void test_changeInterest() {
    	test.startTest();
		Interests__c interest = addInterest('foo');
		Interest__c acc_interest = [SELECT Id, Interest__c from Interest__c WHERE Interest__r.Name = 'Interest0' LIMIT 1];
		acc_interest.Interest__c = interest.Id;
		update acc_interest;
    	test.stopTest();
    	List<String> mylist = new List<String>{'foo', 'Interest1', 'Interest2'};
    	String concat = Util_TMCR.combineList(mylist);
    	Account acc = getAccount();
    	System.assertEquals(concat, acc.Account_Interests_List__c);
    }
    static testMethod void test_deleteInterest(){
    	test.startTest();
		Interests__c interest = [SELECT Id, Name FROM Interests__c WHERE Name = 'Interest0' LIMIT 1];
		delete interest;
    	test.stopTest();
    	List<String> mylist = new List<String>{'Interest1', 'Interest2'};
    	String concat = Util_TMCR.combineList(mylist);
    	Account acc = getAccount();
    	System.assertEquals(concat, acc.Account_Interests_List__c);
    }
    static testMethod void test_undeleteInterest(){
    	test.startTest();
		Interests__c interest = [SELECT Id, Name FROM Interests__c WHERE Name = 'Interest0' LIMIT 1];
		delete interest;
		undelete interest;
    	test.stopTest();
    	List<String> mylist = new List<String>{'Interest0', 'Interest1', 'Interest2'};
    	String concat = Util_TMCR.combineList(mylist);
    	Account acc = getAccount();
    	System.assertEquals(concat, acc.Account_Interests_List__c);
    }
}
Finally, here is the function that concatenates both the expected list and the actual list:
public static String combineList(List<String> items){
        String out = '';
        String delimiter = '\n';
        for(String val : items){
        	out = out + val;
        	if(val.right(1) != delimiter){
        		out = out + delimiter;
        	}
        }
        return out;
    }





 
Best Answer chosen by Jose Boveda 6
pigginsbpigginsb
Here are a couple of things you could try.
1) Update your combineList() method to return out.trim(), which removes whitespace from the start and end of your string. This will remove any straggling new-line characters which your method seems to add at the end.
2) Update your combineList() method to simply return String.join(items,'\n'); This will join your string list into a single value, delimited by the string value specified in the 2nd argument.