You need to sign in to do that
Don't have an account?
Getting System.NullPointerException: Attempt to de-reference a null object error in Test Class
Dear All,
I have developed a class which is calling standard REST API to determine Daily Apex Exceution Limits. I am getting error when I am running test class for the same.
Below is my Apex Class:
Below is my test class:
Below is my mock class:
When I am trying to run tess class I am getting below error trace logs:
Kindly help me to pass the test class.
Many thanks in advance
Thanks & Regards,
Harjeet
I have developed a class which is calling standard REST API to determine Daily Apex Exceution Limits. I am getting error when I am running test class for the same.
Below is my Apex Class:
public class CurrentStorage{ public static void getStorage() { //fetch session od of user String sid = Userinfo.getSessionId(); //fetch Instance URL String baseURL = System.URL.getSalesforceBaseUrl().toExternalForm(); //Building call out to Standard Salesforce Rest API HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(baseURL+'/services/data/v41.0/limits'); req.setHeader('Authorization', 'OAuth '+ sid); Http http = new Http(); HTTPResponse res = http.send(req); Map<String, Object> m = (Map<String,Object>)JSON.deserializeUntyped(res.getBody()); Map<String, Object> dataStorage = (Map<String,Object>)m.get('DailyAsyncApexExecutions'); System.debug('Map: '+ dataStorage); System.debug('Current Storage: ' + dataStorage.get('Remaining')); If(Integer.valueof(dataStorage.get('Max'))*(0.8)<= (Integer.valueof(dataStorage.get('Max')) - Integer.valueof(dataStorage.get('Remaining')))){ Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.toAddresses = new String[] { 'abc@gmail.com','def@gmail.com'}; //message.optOutPolicy = 'FILTER'; message.subject = 'Async Apex Limits Threshold Warning'; message.plainTextBody = 'Your Org Limit of Daily Async Apex Execution reached 80% threshold for '+ baseURL; Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); if (results[0].success) { System.debug('The email was sent successfully.'); } else { System.debug('The email failed to send: ' + results[0].errors[0].message); } } // Send Mail to people } }
Below is my test class:
@isTest public class TestCurrentStorage { static testMethod void validateLocationCallouts() { RestRequest req = new RestRequest(); req.params.put('DailyAsyncApexExecutions', '250000'); //req.params.put('Max', '250000'); req.params.put('Remaining', '200000'); Test.startTest(); Test.setMock(HttpCalloutMock.class, new MockHttpResponse()); CurrentStorage.getStorage(); Test.stopTest(); } }
Below is my mock class:
@isTest global class mockHttpResponse implements HttpCalloutMock{ // Implement this interface method global HTTPResponse respond(HTTPRequest req) { System.assertEquals('GET', req.getMethod()); // Create a fake response HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody('{"Max": "250000","Remaining": "200000"}'); res.setStatusCode(200); res.setStatus('OK'); return res; } }
When I am trying to run tess class I am getting below error trace logs:
Errors: System.NullPointerException: Attempt to de-reference a null object Stack Trace: Class.CurrentStorage.getStorage: Class.TestCurrentStorage.validateLocationCallouts:
Kindly help me to pass the test class.
Many thanks in advance
Thanks & Regards,
Harjeet
Sorry to say but May I know what you did. I am facing issue with test class and it seems to me that you copied and pasted my apex class as your answer which is absolutely working fine.
I need help on test class and I dont know what you are providing to me
I checked and compare your code and found changes which you have made. You have commented below lines from my class
Also, you included one debug statement :
So i ran test class after modifying code as mentioned by you and definitely test class is getting passed. But I have some very big question which needs clarifications from you
1. Why you commented Map<String, Object> dataStorage = (Map<String,Object>)m.get('DailyAsyncApexExecutions'); line from my class
2. That line which you have commented is checking daily async apex exceution limit and I am suspecting since you have commented the line "WILL IT HAVE AN EFFECT ON FUNCTIONALITY"
3. Code coverage sits at 61% and below lines still not covered as yesterday
If you answer my question no 1 and 2 which is definitely bothering me whether changes made by you have an affect on my existing class functionlity and help me to increase my code coverage as sugeted point no 3 then I will definitely choose your answer as best answer.
Many thanks in advance
Thanks & Regards,
Harjeet
Thanks for your quick response. I copie dmodified code as posted by you above and run the code from anonymous block using CurrentSTORAGE.getStorage(); but it is returning error FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object
Kindly help
Thanks & Regards,
Harjeet
Error which I mentioned above is throwing for below mentioned line:
and i checked debug logs generated by anonymouus block and found below;
Kindly help