1

我正在编写 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' ;

        NotificationRestService.retrievingNotificationSettings();

错误是

System.NullPointerException:尝试取消引用空对象 Class.NotificationRestService.retrievingNotificationSettings:第 46 行,第 1 列 Class.Feedpost9.testRest:第 23 行,第 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 应该返回空列表。请有人帮忙!

4

1 回答 1

0

添加@isTest(SeeAllData=true) 注解

@isTest(SeeAllData=true)
global class Feedpost9 {

}
于 2013-01-27T06:54:00.823 回答