function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cooldamselcooldamsel 

[Urgent!!!] Test Class for without sharing class

Hi,

I am a newbie in saleforce. Please help me to complete the below one.

I have an apex class which is a without sharing class. I need to write test class for that without sharing class. Whenever i run the test class, the class is getting failed and showing the error as 'Attenmpt to dereference a null object' in the place where am initiating my apex class.

Please help me out.

Regards,
Indhu R
Best Answer chosen by cooldamsel
Vamsi KrishnaVamsi Krishna
then use the below json string as input to the method.. i just replaced CompanyContacts as items and contact as id..

String json = '{'+
    '    \"items\": ['+
    '        {'+
    '            \"id\": \"pn0\",'+
    '            \"postalcode\": \"0\",'+
    '            \"contactnumber\": \"pc0\"'+
    '        },'+
    '        {'+
    '            \"id\": \"pn1\",'+
    '            \"postalcode\": \"1\",'+
    '            \"contactnumber\": \"pc1\"'+
    '        },'+
    '        {'+
    '            \"id\": \"pn2\",'+
    '            \"postalcode\": \"2\",'+
    '            \"contactnumber\": \"pc2\"'+
    '        }'+
    '    ]'+
    '}';

All Answers

Vamsi KrishnaVamsi Krishna
Its nothing to do with the sharing. can you share your code so that we can check why/where the error is thrown..
cooldamselcooldamsel
Hi Vamsi,

My apex class is:

public without sharing class EF_RequestedResourceDecisionCriteria{

    //Get data from the REquestedresource form field data field
    public static void SetDecisionCriteria(EF_Requested__c[] requestedApplications)
    {
        for (EF_Requested__c r:requestedApplications){
        //Parse the Json data
           
        }
    //Check if there was any criteria that matched the Criteria defined in Salesforce
    //Save the criteria and its data on the RequestedResource Object

    }
    //method to parse the Json data
    public void parseJson(string rrFormData) {
        Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(rrFormData);
        List<Object> items = (List<Object>)root.get('items');
        for (Object item : items) {
            Map<String, Object> i = (Map<String, Object>)item;
            System.debug(i.get('id'));
        }
    }
}

The test class i wrote is:

@isTest
public class EF_RequestedResourceDecisionCriteriaTest
{
    static testMethod void RequestedResourceDecisionCriteriaTest()
    {
        EF_Resource_Metadata__c objResourceMetaData = new EF_Resource_Metadata__c(Name='Test Resource Meta Data');
        insert objResourceMetaData;
     
        EF_Decision_Criteria__c objDecisionCriteria = new EF_Decision_Criteria__c();
       
        objDecisionCriteria.Name = 'Test Decision Criteria';
        objDecisionCriteria.EF_Resource_Metadata__c = objResourceMetaData.id; 
        objDecisionCriteria.CurrencyIsoCode = 'GBP';
       
      
        insert objDecisionCriteria;
       
            String json = '{'+
    '    \"CompanyContacts\": ['+
    '        {'+
    '            \"contact\": \"pn0\",'+
    '            \"postalcode\": \"0\",'+
    '            \"contactnumber\": \"pc0\"'+
    '        },'+
    '        {'+
    '            \"contact\": \"pn1\",'+
    '            \"postalcode\": \"1\",'+
    '            \"contactnumber\": \"pc1\"'+
    '        },'+
    '        {'+
    '            \"contact\": \"pn2\",'+
    '            \"postalcode\": \"2\",'+
    '            \"contactnumber\": \"pc2\"'+
    '        }'+
    '    ]'+
    '}';
       
       Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
       User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
       EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
       LocaleSidKey='en_US', ProfileId = p.Id,
       TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com');
      
       system.runas(u2)
       { 
        EF_RequestedResourceDecisionCriteria objReqResDecCrit = new EF_RequestedResourceDecisionCriteria();
        objReqResDecCrit.parseJson(json);
       
        system.assert(objReqResDecCrit != null);
        }       
    }
}

Thanks in advance.
Vamsi KrishnaVamsi Krishna
are you sure, you are getting error in this line ?
EF_RequestedResourceDecisionCriteria objReqResDecCrit = new EF_RequestedResourceDecisionCriteria();

can you give the complete error message you get when you run the test class ?
cooldamselcooldamsel
Hi Vamsi,

The following is the error am getting while running the test.

Time Started 3/13/2014 5:00 AM
Class EF_RequestedResourceDecisionCriteriaTest
Method Name RequestedResourceDecisionCriteriaTest
Pass/Fail Fail
Error Message System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.EF_RequestedResourceDecisionCriteria.parseJson: line 28, column 1
Class.EF_RequestedResourceDecisionCriteriaTest.RequestedResourceDecisionCriteriaTest: line 40, column 1
Vamsi KrishnaVamsi Krishna
the error is thrown from your parseJson method in your class in the for loop..

Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(rrFormData);
List<Object> items = (List<Object>)root.get('items');

based on your json string, the map root will have the below object. which has CompanyContacts at high level and 3 child nodes..
{
CompanyContacts=(
{contact=pn0, contactnumber=pc0, postalcode=0},
{contact=pn1, contactnumber=pc1, postalcode=1},
{contact=pn2, contactnumber=pc2, postalcode=2})
}

to retrieve the values from the map you have to use the correct key.. otherwise you will end up with null and hence the dereference error..
here is the updated method..

public void parseJson(string rrFormData) {
        Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(rrFormData);
        List<Object> items = (List<Object>)root.get('CompanyContacts');
        for (Object item : items) {
            Map<String, Object> i = (Map<String, Object>)item;
            System.debug(i.get('contact'));
        }   
}

cooldamselcooldamsel
Hi Vamsi,

Thanks for your suggestion. but the thing is i cant update my apex class. All i can do is change the json value in my test class. Is there a way to know wat json value i should pass to my test class?
Vamsi KrishnaVamsi Krishna
then use the below json string as input to the method.. i just replaced CompanyContacts as items and contact as id..

String json = '{'+
    '    \"items\": ['+
    '        {'+
    '            \"id\": \"pn0\",'+
    '            \"postalcode\": \"0\",'+
    '            \"contactnumber\": \"pc0\"'+
    '        },'+
    '        {'+
    '            \"id\": \"pn1\",'+
    '            \"postalcode\": \"1\",'+
    '            \"contactnumber\": \"pc1\"'+
    '        },'+
    '        {'+
    '            \"id\": \"pn2\",'+
    '            \"postalcode\": \"2\",'+
    '            \"contactnumber\": \"pc2\"'+
    '        }'+
    '    ]'+
    '}';
This was selected as the best answer
Vamsi KrishnaVamsi Krishna
let me know if the answers helped to resolve your error by marking the question as solved..  :-)
cooldamselcooldamsel
Hi Vamsi,

Thanku. It helped me cover 71% code.