简体   繁体   English

测试其余Web服务时出现空指针异常

[英]Null pointer exception when testing a rest web service

I get a NullPointerException when trying to test a web service 尝试测试Web服务时收到NullPointerException

My code: 我的代码:

@Path("/")
public class SubscriptionService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/status/{subsId}")
    public Response getSubscriptionStatus(@PathParam("subsId") int subsId) {
        return Response.status(200).entity("subscription status for id: " + subsId).build();
    }
}

Test class: 测试类别:

public class TestService {

    private SubscriptionService service;

    @Test
    public void testGetSubscriptionStatus() {
        final Response response = service.getSubscriptionStatus(5);

        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals("subscription status for id: 5", response.getEntity());
    }
}

The error appears here: final Response response = service.getSubscriptionStatus(5); 错误出现在这里: final Response response = service.getSubscriptionStatus(5);

Does anyone have any idea why I get this error? 有谁知道为什么我会收到此错误?

You never initialize service . 您永远不会初始化service So you get an NPE. 这样就得到了NPE。

change: 更改:

private SubscriptionService service;

To: 至:

private SubscriptionService service = new SubscriptionService();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tomcat:Null Pointer尝试访问Web服务时出现异常 - Tomcat: Null Pointer Exception when trying to access web service 从线程调用Web服务时,空指针异常 - Null Pointer Exception when calling web service from a thread 在Java中进行测试时出现空指针异常 - Null pointer exception when testing in Java Spring 引导(REST 服务)集成测试因 null 指针异常而失败 - Spring Boot (REST Service) Integration tests fail with a null pointer exception 使用Web服务从xml文档中获取数据时,出现空指针异常 - I am getting null pointer exception when i get the data from xml document using web service 在Jax-rs Web服务中注入HttpResponse时,空指针异常 - Null Pointer Exception when inject HttpResponse in Jax-rs web service Android BroadcastReceiver in Service在向其发送广播时给出空指针异常 - Android BroadcastReceiver in Service giving null pointer exception when sending broadcast to it Web服务上的空指针异常 - null pointer exception on web services 解析XML网页时,getNodeValue()上的null指针异常-android - null pointer exception on getNodeValue() when parsing XML web page - android Camel REST 服务在返回 null 时抛出异常 - Camel REST service throws Exception when returning null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM