How to Write the Test class for Salesforce Apex Aura Enabled class

0 votes

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();
                    }
                
Mar 15, 2022 in SalesForce by surbhi
• 3,810 points
825 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In SalesForce

0 votes
0 answers

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

In logic app i'm using salesforce connector after deployment ...READ MORE

Mar 1, 2022 in SalesForce by surbhi
• 3,810 points
478 views
0 votes
1 answer

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

you are not passing the parameterValues, use ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,810 points
413 views
0 votes
1 answer

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

You are not passing the parameterValues, use ...READ MORE

answered Mar 9, 2022 in SalesForce by CoolCoder
• 4,360 points
326 views
0 votes
0 answers

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Salesforce Test Class - Facing - System.NullPointerException: ...READ MORE

Mar 1, 2022 in SalesForce by surbhi
• 3,810 points
1,251 views
0 votes
1 answer

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Because testAccount.account is never set, it is ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,810 points
1,114 views
0 votes
1 answer

How to add days to date time in Salesforce Apex?

Beware the DST issue! The "addDays" function ...READ MORE

answered Mar 15, 2022 in SalesForce by CoolCoder
• 4,360 points
2,431 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
2,107 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,050 points
398 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,050 points
262 views
0 votes
1 answer

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Training that covers all ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 294 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP