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

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

问题描述

我正在编写一个在 apex 中的休息服务的单元测试

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 {

}

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

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