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
Ritesh__Ritesh__ 

List.size() method not working the way it expects in testing

i am testing my rest api its working fine i tested on a tool and send request and getting expeected result but when i am testing it with this class its giving me error

 

global class Feedpost9 {

// your methods here...

static testMethod void testRest() {
// set up the request object
System.RestContext.request = new RestRequest();
RestContext.request.requestURI = '/v.9/notifications/preferences/ritesh';
// Invoke the method directly
Member__c member=new Member__c(name='ritesh');
insert member;

Notification_Settings__c no=new Notification_Settings__c(member__c=member.Id);
no.Event__c='Category|Cloud Foundry' ;
no.Event_Per_Member__c='12';
insert no;

NotificationRestService.retrievingNotificationSettings();

//RestContext.request.requestURI = '/v.9/notifications/preferences/rashmi.xml' ;

NotificationRestService.retrievingNotificationSettings();
RestContext.request.requestURI ='/v.9/notifications/preferences/ritesh.xml';
NotificationRestService.retrievingNotificationSettings();

RestContext.request.requestURI='/v.9/notifications/preferences/ritesh.xml';
RestContext.request.addparameter('fields','id,name,Do_Not_Notify__c,Event__c,Event_Per_Member__c,Notification_Method__c,Member__r');
NotificationRestService.retrievingNotificationSettings();
System.RestContext.request=new RestRequest();
RestContext.request.requestURI='/v.9/notifications/preferences/rashmi';
NotificationRestService.retrievingNotificationSettings();
}
}

 

 

i am getting error in testing at this line

 

 

RestContext.request.requestURI='/v.9/notifications/preferences/rashmi';
NotificationRestService.retrievingNotificationSettings();

 

and segment of my RestService class is

@RestResource(urlMapping='/v.9/notifications/preferences/*')
global class NotificationRestService
{
@HttpGet
global static String retrievingNotificationSettings(){

RestRequest req=RestContext.request;

RestResponse res=RestContext.response;

List<Notification_Settings__c> note1=new List<Notification_Settings__c>();
note1.addAll([SELECT id,name,Member__r.name,Do_not_Notify__c,Event__c,Event_Per_Member__c,Notification_Method__c from Notification_Settings__c WHERE

note1.clear();

Member__r.name= :userName Limit 1]);

if( note1.size() >0 )
{
note=note1.get(0);
}

 

in line note1.size() i am getting Null pointer exception when i check for wheather note1.is null or not then its passing and showing that note1 is not null if i change this if statement to 

if(note1!=null){

if(note1.size() >0)

note=note1.get(0);}

 

if i omit conditional if statement then and call directly note1.get(0) then i am getting index out of bound exception expecting NullPointer excetion. can any one please explain weired behaviour of List class in testing when i run this url its work fine no null pointer exception i know size of list is zero but i am checking for negative case in testing there should be no null pointer exception i think please explain this behaviour of List class??

ibtesamibtesam

List.size() is working fine i guess,

note1 too looks fine, can you tell me what is "note" ?? you did not initialized that probably ??

Ritesh__Ritesh__

note is a variable with type Notification_Settings__c  and i declared it as simply

Notification_Settings__c note =new Notification_Settings__c();

 

. there is some type mistake in previous post now i corrected it(wrote note in place of note1 so now its correct) please see it again and reply. but still it should not throwing a NullPointerException because i am only assigning it f

sivaextsivaext

Hi 

 

is query executing correct? 

 

 what is note1.clear();? is in between query ?