测试中出现意外的Null Pointer异常 [英] Unexpected Null Pointer exception in testing

查看:259
本文介绍了测试中出现意外的Null Pointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用顶点编写休息服务的单元测试

i am writing a unit test of a rest service in apex

我的测试班是

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' ;

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

当测试单元到达这一点时,我得到了一个错误 RestContext.request.requestURI ='/v.9/notifications/preferences';

when test unit reach at this point then i got an error RestContext.request.requestURI = '/v.9/notifications/preferences' ;

        NotificationRestService.retrievingNotificationSettings();

错误是

System.NullPointerException:尝试取消引用空对象 Class.NotificationRestService.retrievingNotificationSettings:第46行,第1列Class.Feedpost9.testRest:第23行,第1列

System.NullPointerException: Attempt to de-reference a null object Class.NotificationRestService.retrievingNotificationSettings: line 46, column 1 Class.Feedpost9.testRest: line 23, column 1

我遇到此错误的部分是

 List<Notification_Settings__c> note1=[SELECT id,name,Member__r.name,Do_not_Notify__c,Event__c,Event_Per_Member__c,Notification_Method__c from Notification_Settings__c WHERE Member__r.name= :userName Limit 1];
Notification_Settings__c note;

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

并且错误在行{if(note1.size()> 0) 我无法确定note1是否不等于null,那么它将进入下一行,但是在下一行中,给我一个错误,为什么下一行中的note1为null并首先通过if statement.query应该返回空列表.请有人帮助!!

and error is on the line {if(note1.size() >0) i am unable to figure if note1 is not equal to null then it will come to next line but in next line it is giving me error why note1 is null in next linel and passing first if statement.query should return empty list .please someone help!!

推荐答案

添加@isTest(SeeAllData = true)注释

Add @isTest(SeeAllData=true) annotation

@isTest(SeeAllData=true)
global class Feedpost9 {

}

这篇关于测试中出现意外的Null Pointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆