You need to sign in to do that
Don't have an account?
Siva Sakthi
Test class for dynamic soql query
Hi All,
This below test class covers 36% only how to increase the coverage more than 75%. Please guide me to get more coverage. Thanks in advance.
This below test class covers 36% only how to increase the coverage more than 75%. Please guide me to get more coverage. Thanks in advance.
Apex Class: ========= public with sharing class MultiSelectLookupController { @AuraEnabled public static List<RecordsData> fetchRecords(String objectName, String filterField, String searchString, String values) { try { List<RecordsData> recordsDataList = new List<RecordsData>(); List<String> selectedvalues = (List<String>) JSON.deserialize(values, List<String>.class); String query = 'SELECT Id, ' + filterField + ' FROM '+objectName; if(selectedvalues != null && selectedvalues.size() > 0) { query += ' WHERE Id IN: selectedvalues LIMIT 49999'; } else { query += ' WHERE '+filterField+ ' LIKE ' + '\'' + String.escapeSingleQuotes(searchString.trim()) + '%\' LIMIT 49999'; } for(SObject s : Database.query(query)) { recordsDataList.add( new RecordsData((String)s.get(filterField), (String)s.get('id')) ); } return recordsDataList; } catch (Exception err) { if ( String.isNotBlank( err.getMessage() ) && err.getMessage().contains( 'error:' ) ) { throw new AuraHandledException(err.getMessage().split('error:')[1].split(':')[0] + '.'); } else { throw new AuraHandledException(err.getMessage()); } } } public class RecordsData { @AuraEnabled public String label; @AuraEnabled public String value; public RecordsData(String label, String value) { this.label = label; this.value = value; } } } Test Class: ======== @isTest public class MultiSelectLookupControllerTest { @istest static void QLIeditMethod_Test(){ List<Quote> quoteList= [select id from quote]; string recType = 'Commercial products'; Product2 prod = new Product2(); prod.Name = 'Test Bulk'; String objectName = 'Product2'; String filterField = 'Name'; String searchString = 'unit Bulk'; String values = 'Name'; //String myJSON = JSON.stringify(prod); Test.startTest(); try { MultiSelectLookupController.fetchRecords(objectName,filterField,searchString,values); }Catch(exception e){} Test.stopTest(); } public static testMethod void testGetProductValues() { MultiSelectLookupController controller = new MultiSelectLookupController(); } }
Hi Siva,
Please go through the solution. Actually, I find it quite difficult to write a test class so I want you to change some code of Catch Block to write a test class
Changes : All lines of code that are in Catch Block convert it into a single line so that test coverage increases.(see line 18 in above picture)
Class
Test Class:
Please do the needful changes according to your code.
Please let me know my test class is working or not??
If it helps you please mark it as Best Answer so that other people would take reference from it.
Thank You!
All Answers
Hi Siva,
Please go through the solution. Actually, I find it quite difficult to write a test class so I want you to change some code of Catch Block to write a test class
Changes : All lines of code that are in Catch Block convert it into a single line so that test coverage increases.(see line 18 in above picture)
Class
Test Class:
Please do the needful changes according to your code.
Please let me know my test class is working or not??
If it helps you please mark it as Best Answer so that other people would take reference from it.
Thank You!