You need to sign in to do that
Don't have an account?

test class for lwc calling apex class won't work
Hi All,
Can you help us with a test class? We already confirmed the apex class works. Do you see anything wrong with the test class?
We are using the class in a LWC and we confirmed that it works when we test it manually. The LWC is using a wire to get the entitlementRec object data.
The coverage test stops at around at line 9 after the json serialize lines.
The error is: System.NullPointerException: Attempt to de-reference a null object Class.AssetsQuery.getAssetsOwnedForEntList: line 9, column 1 Class.AssetsAndEntitlementsQuery_Test.assetsTest: line 52, column 1
The log says: 14:10:19.129 (3033421382)|VARIABLE_SCOPE_BEGIN|[7]|fieldmapObj|Map|true|false 14:10:19.129 (3033433056)|VARIABLE_ASSIGNMENT|
apex class
test class
Can you help us with a test class? We already confirmed the apex class works. Do you see anything wrong with the test class?
We are using the class in a LWC and we confirmed that it works when we test it manually. The LWC is using a wire to get the entitlementRec object data.
The coverage test stops at around at line 9 after the json serialize lines.
The error is: System.NullPointerException: Attempt to de-reference a null object Class.AssetsQuery.getAssetsOwnedForEntList: line 9, column 1 Class.AssetsAndEntitlementsQuery_Test.assetsTest: line 52, column 1
The log says: 14:10:19.129 (3033421382)|VARIABLE_SCOPE_BEGIN|[7]|fieldmapObj|Map|true|false 14:10:19.129 (3033433056)|VARIABLE_ASSIGNMENT|
apex class
public without sharing class AssetsQuery { @AuraEnabled(cacheable=true) public static Asset[] getAssetsOwnedForEntList(Id recordId, Object entitlementRec) { System.debug('In getAssetsOwnedForEntList'); system.debug('entitlementRec'+entitlementRec); Map<String,Object> mapObj = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(entitlementRec)); Map<String,Object> fieldmapObj = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(mapObj.get('fields'))); system.debug('mapObj'+mapObj); Map<String,Object> accfieldmap = (Map<String,Object>)fieldmapObj.get('AccountId'); Map<String,Object> prodfieldmap = (Map<String,Object>)fieldmapObj.get('Product__c'); system.debug('value'+accfieldmap.get('value')); String accId = (String)accfieldmap.get('value'); String prodId = (String)prodfieldmap.get('value'); return [ SELECT Id, Name, Account.Name, Engine_Model__r.Name, Product2.Name, Operator_lookup__r.Name, Status FROM Asset WHERE AccountId = :accId AND (Engine_Model__c = :prodId OR Product2Id = :prodId) AND Status <> null ORDER BY Name ASC ]; } }
test class
@isTest(seeAllData=true) Public class AssetsAndEntitlementsQuery_Test{ static testMethod void assetsTest(){ Map<String,String> mprecordProduct=new Map<String,String>(); FOR(RecordType rt:[SELECT Id, Name FROM RecordType WHERE SobjectType = 'Product2']) { mprecordProduct.put(rt.Name,rt.id); } Test.startTest(); Product2 prd1 = new Product2(); prd1.Name='CF34-10A'; prd1.RecordTypeId=mprecordProduct.get('Engine Model'); prd1.IsActive=true; Insert prd1; Account acc1 = new Account(); acc1.name = 'LMNOP Airways'; acc1.Account_Type__c = 'CEO - Standard'; Insert acc1; Entitlement ET1 = New Entitlement(); ET1.Name='GTA LMNOP Airways'; ET1.Comments__c='Third Parties'; ET1.GTA_Number__c='12-12-12'; ET1.StartDate=System.Today(); ET1.GTA_Type__c='First Tier'; ET1.AccountId=acc1.Id; ET1.Product__c=prd1.Id; ET1.EndDate=System.today()+365; Insert ET1; Asset as1 = New Asset(); as1.Name='LMNOP 123'; as1.AccountId=acc1.Id; as1.Operator_lookup__c=acc1.Id; as1.Product2Id=prd1.Id; as1.Engine_Model__c=prd1.Id; as1.Status='In Operation'; Insert as1; AssetsQuery.getAssetsOwnedForEntList(ET1.Id,ET1); Test.stopTest(); } }
Greetings!
As per the error message,I can see that you are trying to use the list variable which doesn't have records.So,can you please try to capture the debug logs while running the test class to see,which query is returning 0 records.
Reference:https://help.salesforce.com/HTViewSolution?id=000063739
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Warm Regards,
Shirisha Pathuri