You need to sign in to do that
Don't have an account?
KSKumaar
Test class is not covered completely. Giving exception: NullPointerException
I wrote a Rest api class and it is given below.
And i wrote test class above class and this test class is covering for get method and giving exception in put method i.e,
Test class:
Thanking you
@RestResource(urlMapping='/threeobjects/*') Global class MyRest_ThreeObjects_Getting { @HttpGet global static List<MultiWrapper> doGet(){ List<MultiWrapper> listmw = new List<MultiWrapper>(); RestRequest req = RestContext.request; RestResponse res = RestContext.response; Map<Id, User> contactToUser = new Map<Id, User>(); for (User u : [select Id, FirstName, LastName, UserName, Email,ContactId from User where ContactId != null limit 1] ) { contactToUser.put(u.ContactId, u); } for (Contact c : [select Id, AccountId, Lastname, Firstname, Email, Account.Id, Account.Name, Account.Phone from Contact where Id in :contactToUser.keySet()]) { listmw.add(new MultiWrapper(contactToUser.get(c.Id), c.Account, c)); } return listmw; } Global class MultiWrapper { User us; Account acc; Contact con; Public MultiWrapper (User us, Account acc, Contact con){ this.us = us; this.acc = acc; this.con = con; } } @HttpPut global static String doPut(){ List<MultiWrapper> result = doGet(); String jsonstring = json.serialize(result); User us; Account acc; Contact con; list<MultiWrapper> MultiWrapperList = (list<MultiWrapper>)JSON.deserialize(jsonstring,list<MultiWrapper>.class); for(MultiWrapper mm : MultiWrapperList){ us = mm.us; acc = mm.acc; con = mm.con; } System.debug('Before queryl:::'+us); us = [select Alias from User where id=:us.Id]; us.Alias = 'Exam'; update us; System.debug('After query:::'+us); return 'User successfully updated'; } }
And i wrote test class above class and this test class is covering for get method and giving exception in put method i.e,
System.NullPointerException: Attempt to de-reference a null object
Test class:
@IsTest Private class Test_ThreeObjects_Getting { static testMethod void testDoGet() { RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); Account acc = new Account(Name='aa'); insert acc; Contact con = new Contact(LastName='con aa', AccountId=acc.id); insert con; User u = new User(Lastname = 'Test Name',Alias='some',Email='some@soem.com', Username='some@some.in',CommunityNickname='some',ProfileId='00e28000000OBjS', ContactID = con.ID,TimeZoneSidKey='America/New_York',LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1',LanguageLocaleKey='en_US'); insert u; req.requestURI='/services/apexrest/threeobjects'; req.httpMethod = 'GET'; RestContext.request = req; RestContext.response = res; MyRest_ThreeObjects_Getting.MultiWrapper mulWrap = new MyRest_ThreeObjects_Getting.MultiWrapper(u,acc,con); List<MyRest_ThreeObjects_Getting.MultiWrapper> results = MyRest_ThreeObjects_Getting.doGet(); } static testMethod void testDoPut() { RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI='/services/apexrest/threeobjects'; req.httpMethod = 'PUT'; RestContext.request = req; RestContext.response = res; User us = [select Alias from User where id='00528000004fOcq']; us.Alias = 'some'; update us; String results1 = MyRest_ThreeObjects_Getting.doPut(); //Giving error here System.assertEquals(results1,'User successfully updated'); } }Could anyone please tell me why i am facing this exception and please help me to solve this.
Thanking you
HI KS,
Modify your Put method as follows. It does not handling the any erros if it comes. and i modify the put method with bulkfication.
and the test method for above method is as follows.check with these answers and let me know
Thank you.
All Answers
Hi
@NABEEL KT
In rest class, error is comng at line number 42 and in test class error is coming at line number 36
Please see test class, i already mentined that
try above code
Try this test class also
I tried with test class which is provided by you. Even though it is giving same exception at same line numbers
HI KS,
Modify your Put method as follows. It does not handling the any erros if it comes. and i modify the put method with bulkfication.
and the test method for above method is as follows.check with these answers and let me know
Thank you.