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
Afzaal HassanAfzaal Hassan 

Test class getting System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set Error

Hello Everyone,
I have written a trigger and handler class (which uses a future method) and it works well. I am really struggling to write the test class for it. My first problem is that the code coverage is high (before tweaks it was 0% in the handler class). After tweaking, I am getting the following error:
System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
I am not sure how to solve this and I am a pretty new dev so not sure what to do next . Can someone please help?
Here is my code:
Trigger:
trigger AccountTriggerKeepAcctTeam on Account (before update) {
    List<AccountTeamMember> listAcc = [SELECT Id, AccountAccessLevel, AccountId, CaseAccessLevel, UserId, ContactAccessLevel, OpportunityAccessLevel, TeamMemberRole, PhotoUrl, Title FROM AccountTeamMember Where AccountId in : Trigger.new AND TeamMemberRole != 'Account Executive']; 
    for(Account acc: Trigger.new){
        Account oldAccount = Trigger.oldMap.get(acc.Id);
        if(acc.OwnerId != oldAccount.OwnerId){
            system.debug('AccountTeamMember records: '+(JSON.serialize(listAcc))); 
            String str = JSON.serialize(listAcc); 
             //delete team member records if required
            AccountTriggerKeepAcctTeamHandler.retainOldTeamMemberOnOwnerChange(str);   
        }
    }
    
}

Handler:
public class AccountTriggerKeepAcctTeamHandler {
    @future
    public static void retainOldTeamMemberOnOwnerChange(String str){
        system.debug('Future call '+str); 
        List<AccountTeamMember> newlistAcc = (List<AccountTeamMember>) JSON.deserialize(str,List<AccountTeamMember>.class);
        for(AccountTeamMember objAccTeamMember : newlistAcc){
            objAccTeamMember.Id= null;
        }
        system.debug('Account records to insert'+(JSON.serialize(newlistAcc)));
        Upsert newlistAcc;
     }
}

Test class:
@isTest
public class AccountTriggerKeepAcctTeamTest {
    //@TestSetup
    
    static testMethod void AcctOwnerChange(){
        List<User> userList = TestDataFactory.createUser(true, 2);
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u1 = new User(Alias = 'standt', Email='standarduser@testorg1.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg01.com');
        
        insert u1;

        User u2 = new User(Alias = 'standt', Email='standarduser@testorg2.com', 
            EmailEncodingKey='UTF-8', LastName='Testing2', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg92.com');
        
        insert u2;

        System.runAs(u1){
            Allowed_Account_Owner_Change__c setting = new Allowed_Account_Owner_Change__c();
            setting.Allowed_to_change_Account_Owner__c = true;
            insert setting;
            fferpcore__ExchangeRateGroup__c exrg = new fferpcore__ExchangeRateGroup__c(CurrencyISOCode = 'USD', fferpcore__DefaultCurrency__c = 'USD - U.S. Dollar', Name = 'FF Shared Test Group', fferpcore__SelectedCurrencies__c = 'USD - U.S. Dollar');
            insert exrg;
            c2g__codaCompany__c company = new c2g__codaCompany__c();
            company.Name = 'ApexTestCompany';
            company.RecordTypeId = Schema.SObjectType.c2g__codaCompany__c.RecordTypeInfosByName.get('SUT').RecordTypeId;
            insert company;
            company.c2g__ExchangeRateGroup__c = exrg.Id;
            update company;

            Account acc = new Account(Name = 'Test Acc2', NumberOfEmployees = 500);//TestDataFactory.createAccountwithCurrencyMaster(true);
            acc.OwnerId = userinfo.getUserId();
            insert acc;
            
            AccountTeamMember accTeam   = new AccountTeamMember();
            accTeam.UserId                  =  acc.OwnerId;
            accTeam.AccountAccessLevel  =  'Read';
            accTeam.AccountId           =  acc.Id;
            insert accTeam;
            
            System.debug('## User '+userinfo.getUserName());
            //create opportunity

            
        //}        

   // }
    
  //  static testMethod void testAcctOwnerChange(){
       // User u = [Select id, LastName from User where LastName = 'Testing2'];
       // Account acc = [Select id, OwnerId from Account Limit 1];
       Test.startTest();
        acc.OwnerId = u2.id;
        update acc;
        AccountTriggerKeepAcctTeamHandler.retainOldTeamMemberOnOwnerChange(JSON.serialize(acc));
        //System.assertEquals(accTeam.UserId,u2.Id);
        Test.stopTest();
        //AccountTeamMember atm = [Select userId,AccountId, AccountAccessLevel from AccountTeamMember where AccountId =: acc.Id];
        
    }
    
    

}
ravi soniravi soni
hi,
You don't need to write following line into test class.
//AccountTriggerKeepAcctTeamHandler.retainOldTeamMemberOnOwnerChange(JSON.serialize(acc));
please remove it and check your test class I'm sure I give you 100% coverage.


let me know if it helps you and mark it as best answer.
Thank you