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
fiona gentryfiona gentry 

How to get rid off Apex Test class errors?

Hi Pals,

This  test cass keeps throwing an error for

1) For Method "testGetLevel1 " as
System.AssertException: Assertion Failed: Expected: Test Level 1, Actual: null
1) For Method "testsaveCaseType" as
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Level 1: bad value for restricted picklist field: Test Level 1: [Level_1__c]

Here is test class
 
@isTest
public class ERT_MultiLevelSelectEditTest {
  
  @testSetup static void setup() {
        
    Case_Type_Data__c ct = new Case_Type_Data__c();
    ct.Level_1__c = 'Test Level 1';
    ct.Level_2__c = 'Test Level 2';
    ct.Level_3__c = 'Test Level 3';
    insert ct; 

    }
    
  @isTest
   static  void testGetLevel1()
   {
    
    Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
    insert cas;
    ERT_Case_Type__c  obj = new ERT_Case_Type__c ();
    obj.Level_1__c = 'Test Level 1';
    obj.Level_2__c = 'Test Level 2';
    obj.Level_3__c = 'Test Level 3';
    obj.Case__c = cas.id;
    upsert obj;
       
   test.startTest();
   List<ERT_MultiLevelSelectEdit.LevelWrapper> lstWrapper= ERT_MultiLevelSelectEdit.getLevel1(obj.id);
   test.stopTest();
   system.assertEquals('Test Level 1', lstWrapper[0].strL1val);
     
   
     
    

   }

  @isTest
   static void testGetLevel2()
   {
   test.startTest();
   List<String> s = ERT_MultiLevelSelectEdit.getLevel2('Test Level 1');
   test.stopTest();
   system.assertEquals('Test Level 2', s[0]);
      
   

   }
    
  @isTest
   static void testGetLevel3()
   {
    test.startTest();
    List<String> s = ERT_MultiLevelSelectEdit.getLevel3('Test Level 1','Test Level 2');
    test.stopTest();
    system.assertEquals('Test Level 3', s[0]);
   }
    
  @isTest 
   static  void testsaveCaseType(){
   
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
        insert cas;

       /*Insert new Case_Type__c Object*/
       Case_Type__c Ctc = new Case_Type__c();
       Ctc.Level_1__c = 'Test Level 1';
       Ctc.Level_2__c = 'Test Level 2';
       Ctc.Level_3__c = 'Test Level 3';
       Ctc.Case__c = cas.id;
       insert Ctc;
      
     
        ERT_Case_Type__c obj=new ERT_Case_Type__c();
        string one='one';
        string two='two';
        string three='three';
       string oldlevel1='OLDone';
       string oldlevel2='OLDtwo';
       string oldlevel3='OLDthree';
       string guid = 'a3k2F000-0007-7JiA-a3k2-000007JiAQAU';
        test.startTest();
        String testing=ERT_MultiLevelSelectEdit.savecasetype(one,two,three,oldlevel1,oldlevel2,oldlevel3,guid,cas.id);
        test.stopTest();
        system.assertEquals('successfull', testing);
    }
    
 public class LevelWrapper{
        @AuraEnabled
        public string strLevelType{get;set;}
        @AuraEnabled
        public string strL1val{get;set;}
        @AuraEnabled
        public string strL2val{get;set;}
        @AuraEnabled
        public string strL3val{get;set;}
        @AuraEnabled
        public string strERTGuid{get;set;}
        @AuraEnabled
        public boolean isSelected{get;set;}
        
        
    } 
    
    

}

Here is Apex class
 
public class ERT_MultiLevelSelectEdit {
    @AuraEnabled
    public static List<LevelWrapper> getLevel1(string recID){
        List<String> tempLst1 = new List<String>();
        list<LevelWrapper> lstWrap=new list<LevelWrapper>();
        
        list<ERT_Case_Type__c>  lstertcase=new list<ERT_Case_Type__c>();
        lstertcase=[select id,UniqueGUID__c,Level_1__c,Level_2__c,Level_3__c from ERT_Case_Type__c where id=:recID];
        string strL1,strL2,strL3,strGuid;
        if(lstertcase.size()>0){
            strL1=lstertcase[0].Level_1__c;
            strL2=lstertcase[0].Level_2__c;
            strL3=lstertcase[0].Level_3__c;
            strGuid=lstertcase[0].UniqueGUID__c;
        }
       
       LevelWrapper guidObj= new LevelWrapper();
       string strERTGuid1=(string)strGuid;
       guidObj.strLevelType='recGUID';
       guidObj.strERTGuid=strERTGuid1;
       lstWrap.add(guidObj);
        
        for(AggregateResult  ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c  group by Level_1__c]) {
            LevelWrapper obj= new LevelWrapper();
            string strLev1=(string)ar.get('Level_1__c');
            obj.strLevelType='recL1';
            obj.strL1val=strLev1;
            if(strLev1==strL1)
                obj.isSelected=true;
            else
                obj.isSelected=false; 
            lstWrap.add(obj);
            
        }

        for(string ar:getLevel2(strL1)){
            if(strL1!=null){
                LevelWrapper obj= new LevelWrapper();
                
                string strLev2=(string)ar;
                obj.strLevelType='recL2';
                obj.strL2val=strLev2;
                if(strLev2==strL2)
                    obj.isSelected=true;
                else
                    obj.isSelected=false; 
                lstWrap.add(obj);
            }
        }
        
        for(string ar:getLevel3(strL1,strL2)){
            if(strL2!=null){
                LevelWrapper obj= new LevelWrapper();
                
                string strLev2=(string)ar;
                obj.strLevelType='recL3';
                obj.strL3val=strLev2;
                if(strLev2==strL3)
                    obj.isSelected=true;
                else
                    obj.isSelected=false; 
                lstWrap.add(obj);
            }
        }
        System.debug(' wrapper '+lstWrap);
        return lstWrap;
        
        
    } 
    
    @AuraEnabled
    public static List<String> getLevel2(string strName){
        List<String> tempLst2 = new List<String>();
        for(AggregateResult  ar : [select Level_2__c,COUNT(id) from Case_Type_Data__c where Level_1__c=:strName  group by Level_2__c])
        {
            tempLst2.add(''+ar.get('Level_2__c'));
        }
        
        return tempLst2;
        
    } 
    
    @AuraEnabled
    public static List<String> getLevel3(string strName1,string strName2){
        List<String> tempLst3 = new List<String>();
        for(AggregateResult  ar : [select Level_3__c,COUNT(id) from Case_Type_Data__c  where Level_1__c=:strName1 and Level_2__c=:strName2 group by Level_3__c])
        {
            tempLst3.add(''+ar.get('Level_3__c'));
        }
        
        return tempLst3;
        
        
    } 
    
    @AuraEnabled
    public  static String  savecasetype(string level1,string level2,string level3,string oldlevel1,string oldlevel2,string oldlevel3,string guid,string id){
        string strMsg='successfull';
        try{
            ERT_Case_Type__c obj=new ERT_Case_Type__c();
            
            System.debug('CASE  = '+ Obj.Case__c); 
            Obj.Level_1__c=level1;
            System.debug('Level1  = '+ Obj.Level_1__c); 
            Obj.Level_2__c=level2;
            System.debug('Level2  = '+ Obj.Level_2__c); 
            Obj.Level_3__c=level3;
            System.debug('Level3  = '+ Obj.Level_3__c);
 
            if(id.startsWithIgnoreCase('500'))
                Obj.Case__c = id;
            
            else
                obj.id=id;
              upsert  Obj;
            if(oldlevel1 != ''){
            List<Case_Type__c> lstCTD = new List<Case_Type__c>();
            lstCTD=[SELECT id,Level_1__c,Level_2__c,Level_3__c  FROM Case_Type__c WHERE Level_1__c=:oldlevel1 AND Level_2__c=:oldlevel2 
                    AND Level_3__c=:oldlevel3 AND ERTUniqueGUID__c=:guid ];

           

                
            System.debug('Case Type existing data  = '+ lstCTD);
            lstCTD[0].Level_1__c=Obj.Level_1__c;
            
            lstCTD[0].Level_2__c=Obj.Level_2__c;
            
            lstCTD[0].Level_3__c=Obj.Level_3__c;
                
                upsert  lstCTD;
            
          }
            
        
            
        }
        
        catch(Exception ex){
            strMsg='error';
        }
        return strMsg;  
    }
    
    public class LevelWrapper{
        @AuraEnabled
        public string strLevelType{get;set;}
        @AuraEnabled
        public string strL1val{get;set;}
        @AuraEnabled
        public string strL2val{get;set;}
        @AuraEnabled
        public string strL3val{get;set;}
        @AuraEnabled
        public string strERTGuid{get;set;}
        @AuraEnabled
        public boolean isSelected{get;set;}
        
    }
    
    
    
 }

Your help is needed and aappreciated

Fiona


 
Best Answer chosen by fiona gentry
ravi soniravi soni
Hi Fiona,
try this following apex test class.
@isTest
public class ERT_MultiLevelSelectEditTest {


  @testSetup static void setup() {
        
    Case_Type_Data__c ct = new Case_Type_Data__c();
    ct.Level_1__c = 'Test Level 1';
    ct.Level_2__c = 'Test Level 2';
    ct.Level_3__c = 'Test Level 3';
    insert ct; 

    }
    
  @isTest
   static  void testGetLevel1()
   {
    
    Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
    insert cas;
    ERT_Case_Type__c  obj = new ERT_Case_Type__c ();
    obj.Level_1__c = 'Test Level 1';
    obj.Level_2__c = 'Test Level 2';
    obj.Level_3__c = 'Test Level 3';
    obj.Case__c = cas.id;
    obj.UniqueGUID__c = 'test Id';
    upsert obj;
       system.debug(obj.Id);
   test.startTest();
   List<ERT_MultiLevelSelectEdit.LevelWrapper> lstWrapper= ERT_MultiLevelSelectEdit.getLevel1(obj.id);
   test.stopTest();
       system.debug('lstWrapper===> ' + lstWrapper[0].strERTGuid);
   system.assertEquals('test Id', lstWrapper[0].strERTGuid);
     }

  @isTest
   static void testGetLevel2()
   {
   test.startTest();
   List<String> s = ERT_MultiLevelSelectEdit.getLevel2('Test Level 1');
   test.stopTest();
   system.assertEquals('Test Level 2', s[0]);
      
   

   }
    
  @isTest
   static void testGetLevel3()
   {
    test.startTest();
    List<String> s = ERT_MultiLevelSelectEdit.getLevel3('Test Level 1','Test Level 2');
    test.stopTest();
    system.assertEquals('Test Level 3', s[0]);
   }
    
  @isTest 
   static  void testsaveCaseType(){
   
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
        insert cas;
        string one='one';
        string two='two';
        string three='three';
       string oldlevel1='OLDone';
       string oldlevel2='OLDtwo';
       string oldlevel3='OLDthree';
       string guid = 'a3k2F000-0007-7JiA-a3k2-000007JiAQAU';
       /*Insert new Case_Type__c Object*/
       Case_Type__c Ctc = new Case_Type__c();
       Ctc.Level_1__c = oldlevel1;
       Ctc.Level_2__c = oldlevel2;
       Ctc.Level_3__c = oldlevel3;
       Ctc.Case__c = cas.id;
       Ctc.ERTUniqueGUID__c =  guid;
       insert Ctc;
      
     
        //ERT_Case_Type__c obj=new ERT_Case_Type__c();
       
        test.startTest();
        String testing=ERT_MultiLevelSelectEdit.savecasetype(one,two,three,oldlevel1,oldlevel2,oldlevel3,guid,cas.id);
        test.stopTest();
       system.assertEquals('successfull', testing);
    }
    
 public class LevelWrapper{
        @AuraEnabled
        public string strLevelType{get;set;}
        @AuraEnabled
        public string strL1val{get;set;}
        @AuraEnabled
        public string strL2val{get;set;}
        @AuraEnabled
        public string strL3val{get;set;}
        @AuraEnabled
        public string strERTGuid{get;set;}
        @AuraEnabled
        public boolean isSelected{get;set;}
        
        
    } 
    
    

}

let me know if it helps you and marking the best.
Thank you