• K S L
  • NEWBIE
  • 30 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I wrote a Rest api class and it is given below.
 
@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
 
I wrote a Rest api class and it is given below.
 
@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