You need to sign in to do that
Don't have an account?
Akhil Katkam 5
test class for lookup field apex class
Hi developer Community ,
i have an apex class , and also i have test class for this apex class , but it is not covering few lines of code , can anyone please check the test class and help me with the solution
test clas:
these lines are not covering , please help me with any ideas
i have an apex class , and also i have test class for this apex class , but it is not covering few lines of code , can anyone please check the test class and help me with the solution
public with Sharing class lookupfieldController { @AuraEnabled public static List<sObject> GetRecentRecords(String ObjectName, List<String> ReturnFields, Integer MaxResults) { List<Id> recentIds = new List<Id>(); for(RecentlyViewed recent : [SELECT Id FROM RecentlyViewed WHERE Type = :ObjectName ORDER BY LastViewedDate DESC LIMIT :MaxResults]) { recentIds.add(recent.Id); } String sQUERY = 'SELECT Id, '; if (ReturnFields != null && ReturnFields.Size() > 0) { sQuery += String.join(ReturnFields, ','); } else { sQuery += 'Name'; } sQuery += ' FROM ' + ObjectName + ' WHERE Id IN :recentIds'; List<sObject> searchResult = Database.query(sQuery); return searchResult; } @AuraEnabled public static List<sObject> SearchRecords(String ObjectName, List<String> ReturnFields, List<String> QueryFields, String SearchText, String SortColumn, String SortOrder, Integer MaxResults, String Filter) { //always put a limit on the results if (MaxResults == null || MaxResults == 0) { MaxResults = 20; } SearchText = '%' + SearchText + '%'; List <sObject> returnList = new List <sObject> (); String sQuery = 'SELECT Id, '; if (ReturnFields != null && ReturnFields.Size() > 0) { sQuery += String.join(ReturnFields, ','); } else { sQuery += 'Name'; } sQuery += ' FROM ' + ObjectName + ' WHERE '; if (QueryFields == null || QueryFields.isEmpty()) { sQuery += ' Name LIKE :SearchText '; } else { string likeField = ''; for(string field : QueryFields) { likeField += ' OR ' + field + ' LIKE :SearchText '; } sQuery += ' (' + likeField.removeStart(' OR ') + ') '; } if (Filter != null) { sQuery += ' AND (' + Filter + ')'; } if(string.isNotBlank(SortColumn) && string.isNotBlank(SortOrder)) { sQuery += ' ORDER BY ' + SortColumn + ' ' + SortOrder; } sQuery += ' LIMIT ' + MaxResults; System.debug(sQuery); List <sObject> searchResult = Database.query(sQuery); return searchResult; } @AuraEnabled public static List<sObject> GetRecord(String ObjectName, List<String> ReturnFields, String Id) { String sQUERY = 'SELECT Id, '; if (ReturnFields != null && ReturnFields.Size() > 0) { sQuery += String.join(ReturnFields, ','); } else { sQuery += 'Name'; } sQuery += ' FROM ' + ObjectName + ' WHERE Id = :Id'; List<sObject> searchResult = Database.query(sQuery); return searchResult; } @AuraEnabled public static string findObjectIcon(String ObjectName) { String u; List<Schema.DescribeTabResult> tabDesc = new List<Schema.DescribeTabResult>(); List<Schema.DescribeIconResult> iconDesc = new List<Schema.DescribeIconResult>(); for(Schema.DescribeTabSetResult describeTabSetResult : Schema.describeTabs()) { for(Schema.DescribeTabResult describeTabResult : describeTabSetResult.getTabs()) { if(describeTabResult.getSobjectName() == ObjectName) { if( describeTabResult.isCustom() == true ) { for (Schema.DescribeIconResult describeIcon : describeTabResult.getIcons()) { if (describeIcon.getContentType() == 'image/svg+xml'){ return 'custom:' + describeIcon.getUrl().substringBetween('custom/','.svg').substringBefore('_'); } } } else { return 'standard:' + ObjectName.toLowerCase(); } } } } return 'standard:default'; } @AuraEnabled public static objectDetails getObjectDetails(String ObjectName) { objectDetails details = new objectDetails(); Schema.DescribeSObjectResult describeSobjectsResult = Schema.describeSObjects(new List<String>{ObjectName})[0]; details.label = describeSobjectsResult.getLabel(); details.pluralLabel = describeSobjectsResult.getLabelPlural(); details.iconName = findObjectIcon(ObjectName); return details; } public class objectDetails { @AuraEnabled public string iconName; @AuraEnabled public string label; @AuraEnabled public string pluralLabel; } }
test clas:
@isTest public class lookupfieldController_Test { //This test class just ensures that there is enough code coverage //to get the component into production from your sandbox //it does not perform any validations. static testMethod void testLookupField() { List<string> returnFields = new List<string> {'Name'}; Account acc = new Account(); acc.Name='test'; acc.Industry='Banking'; insert acc; Account a = [SELECT Id,Name FROM Account LIMIT 1]; lookupfieldController.getObjectDetails('Account'); lookupfieldController.GetRecentRecords('Account', returnFields, 5); lookupfieldController.SearchRecords('Account', returnFields, returnFields, '', 'Name', 'ASC', 5, 'CreatedDate > 2001-01-01T00:00:01Z'); lookupfieldController.GetRecord('Account', returnFields, a.Id); } }
these lines are not covering , please help me with any ideas
Please follow the below code, this will cover 93% :- Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
All Answers
Please follow the below code, this will cover 93% :- Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
these lines are not covering,
thanks in advance