-1

stuck in here to write a test class,please suggest a test class ,below test class which was written didn't solved the purpose

                @isTest
                public class testStack {
                    public static  testMethod void testCallClass(){

                     Account acc = new Account(Name='Test Account');
                                    insert acc;
                                    Test.startTest();
                                    LookupSearchResult lcr = new LookupSearchResult(acc.id,'Account','Test Icon','Test Title','Test Sub-Title');
                                    system.assert(lcr.getId() == acc.id);
                                    system.assert(lcr.getSObjectType() == 'Account');
                                    system.assert(lcr.getIcon() == 'Test Icon');
                                    system.assert(lcr.getTitle() == 'Test Title'); 
                                    system.assert(lcr.getSubtitle() == 'Test Sub-Title'); 
                                    Case_Type_Data__c ctd = new Case_Type_Data__c(Name='500J000000Mo18fIAB',Level_1__c='Acct management issues',Level_2__c='Issue1',Level_3__c='Issue4');
                                    insert ctd;
                                 List < List < SObject > > searchResults = Stack.search( '500J000000Mo18fIAB',acc.id );  
                        List < Case_Type_Data__c > listctd = searchResults.get( 0 );  
                        system.assertEquals( 0, listctd.size() );
                            
                                    Test.stopTest();
                    }
                }

here is the aura enabled class for which test class is needed

        public class Stack {
            @AuraEnabled(cacheable=true)
            public static List<LookupSearchResult> search(String searchTerm, List<String> selectedIds){
                if(String.isBlank(searchTerm) || searchTerm.length() < 2){
                    return null;
                }
                String t = '%' + searchTerm + '%'; // decide how you want to search, "starts with", "includes" or what
                
                List<Case_Type_Data__c> records = [SELECT Id, Name, Level_1__c, Level_2__c, Level_3__c
                    FROM Case_Type_Data__c
                    WHERE Level_1__c LIKE :t OR Level_2__c LIKE :t OR Level_3__c LIKE :t
                    ORDER BY Level_1__c, Level_2__c, Level_3__c
                    LIMIT 20];
                
                /* You could also experiment with SOSL?
                records =  [FIND :('*' + searchTerm + '*') IN ALL FIELDS 
                    RETURNING Case_Type_Data__c(Id, Name, Level_1__c, Level_2__c, Level_3__c)][0];
                */
                
                List<LookupSearchResult> results = new List<LookupSearchResult>();
                for(Case_Type_Data__c ctd : records){
                    results.add(new LookupSearchResult(ctd.Id, 'Case_Type_Data__c', 'standard:case_wrap_up', ctd.Name,
                        String.join(new List<String>{ctd.Level_1__c , ctd.Level_2__c, ctd.Level_3__c}, '; ')
                    ));
                }
                return results;
            } 

        }

Previously written tests for PickListHandler which tests all levels namely level1 level2 and level3

                @IsTest
                public class testGetAllLevels { 

                @IsTest
                static  void testGetLevel1()
                {
                    Case_Type_Data__c obj = new Case_Type_Data__c();
                    obj.Level_1__c = 'Test Level 1';
                    insert obj;
                    List<String> s = PickListHandler.getLevel1();

                }

                @IsTest
                static void testGetLevel2()
                {
                    Case_Type_Data__c obj = new Case_Type_Data__c();
                    obj.Level_1__c = 'Test Level 1';
                    insert obj;
                    List<String> s = PickListHandler.getLevel2('Test Level 1');

                }
                    
                @IsTest
                static void testGetLevel3()
                {
                    Case_Type_Data__c obj = new Case_Type_Data__c();
                    obj.Level_1__c = 'Test Level 1';
                    obj.Level_2__c = 'Test Level 2';
                    obj.Level_3__c = 'Test Level 3';
                    insert obj;
                    List<String> s = PickListHandler.getLevel3('Test Level 1','Test Level 2');

                }
                    
                @IsTest 
                static  void testsaveCaseType(){
                        // Create the Case Record.
                        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
                        insert cas;
                       
                        ERT_Case_Type__c obj=new ERT_Case_Type__c();
                        string one='one';
                        string two='two';
                        string three='three';
                        test.startTest();
                        String testing=PickListHandler.savecasetype(one,two,three,cas.id);
                        test.stopTest();
                    }
                    

                    
                 
                    
                }

Thanks in advance Carolyn

Carolyn Cordeiro
  • 1,525
  • 3
  • 11
  • 26
  • Anything you can salvage from test for original functionality? You need something that creates sample Case Type Data records, then runs the search and checks if what was returned matches expectations – eyescream Oct 14 '20 at 13:35
  • @eyescream tried adding creating case type data records and then run search but still zero code coverage,please help – Carolyn Cordeiro Oct 14 '20 at 20:01
  • I mean do you have original test for `PicklistHandler` (https://stackoverflow.com/questions/64129038/how-to-implement-full-search-in-case-type-using-salesforce)? Post it if you can, it'd be easier than creating from scratch. – eyescream Oct 14 '20 at 20:52
  • @eyescream posted in this question – Carolyn Cordeiro Oct 14 '20 at 22:36

1 Answers1

1

That's a low quality test you have there, doesn't really check if the search runs OK. It was written as a bare minimum effort, just to get required code coverage.

Try this (you renamed the class to just "Stack", right? That's OK. I add the question numbers to them, otherwise I'd go crazy ;))

@isTest
public with sharing class Stack64348072Test {

    @isTest
    static void testSearch(){
        insert new List<Case_Type_Data__c>{
          new Case_Type_Data__c(Level_1__c = 'AAA', Level_2__c = 'BBB', Level_3__c = 'CCC'),
          new Case_Type_Data__c(Level_1__c = 'BBB', Level_2__c = 'BBB', Level_3__c = 'CCC'),
          new Case_Type_Data__c(Level_1__c = 'BBB', Level_2__c = 'BBB', Level_3__c = 'BBB'),
          new Case_Type_Data__c(Level_1__c = 'lvl 1', Level_2__c = 'lvl 2', Level_3__c = 'lvl 3'),
          new Case_Type_Data__c(Level_1__c = 'Some longer phrase', Level_2__c = 'to test if middle of the word', Level_3__c = 'works OK too')
        };
            
        Test.startTest();
        // First some negative test cases
        List<LookupSearchResult> result = Stack64129038.search(null, null);
        System.assertEquals(null, result, 'If nothing was sent - no results will be returned');
        
        result = Stack64129038.search('a', null);
        System.assertEquals(null, result, 'We need at least 2 characters to run the search');
        
        result = Stack64129038.search('Some unexpected text', null);
        System.assertEquals(0, result.size(), 'This phrase is not in the reference data we created so we expect no hits.');
        
        // And now some positive cases
        result = Stack64129038.search('AA', null);
        System.assertEquals(1, result.size(), 'There should be exactly 1 match');
        System.assertEquals('AAA; BBB; CCC', result[0].getSubtitle(), 'The "subtitle" should be composed of all 3 levels');
        
        result = Stack64129038.search('BB', null);
        System.assertEquals(3, result.size(), 'There should be 3 matches');
        
        result = Stack64129038.search('middle', null);
        System.assertEquals(1, result.size(), 'There should be 1 match');
        System.assertEquals('Some longer phrase; to test if middle of the word; works OK too', result[0].getSubtitle(), 'The "subtitle" should be composed of all 3 levels');
    }
}
eyescream
  • 18,088
  • 2
  • 34
  • 46