JerseyTest 框架与 Mockito - 拒绝连接:connect

问题描述

我正在使用 Jersey 和 Spring Boot 构建 REST-API(以启动服务器)。 我需要做一些单元测试,我要使用 JerseyTest-Framework (v2.33)。

这是我的测试课。

@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.MOCK)
@TestPropertySource(properties = "server.port=8443")
public class NotificationTest extends JerseyTest {

    @Override
    protected Application configure() {
        return new ResourceConfig(NotificationResource.class);
    }

    @Test
    public void test_getABunchOfNotifications() throws URISyntaxException,sqlException {
        
        ArrayList<Notification> notifications = new ArrayList<>();
        notifications.add(new Notification(1,"notif1",17));
        notifications.add(new Notification(2,"notif2",17));
        notifications.add(new Notification(3,"notif3",17));
        notifications.add(new Notification(4,"notif4",17));
        
        
        
        Response response = Response
            .ok(notifications,MediaType.APPLICATION_JSON)
            .build();
        
        NotificationResource resource = Mockito.mock(NotificationResource.class);
        when(resource.getNotification()).thenReturn(notifications);
        
        Client client = ClientBuilder.newClient();
        
        WebTarget webTarget = client.target("https://localhost:8443/api").path("/notifications");
        
        Response eResponse = webTarget.request().get();
        assertEquals(200,eResponse.getStatus());
           
    }
}

我正在尝试测试 REST 端点(受保护)。我模拟了资源的预期响应,但请求没有正确进行... 我收到此错误,指向这行代码 Response eResponse = webTarget.request().get(); :

javax.ws.rs.ProcessingException: java.net.ConnectException: Connection refused: connect
          at be.ac.umons.g04.api.core.resources.NotificationTest.test_getABunchOfNotifications(NotificationTest.java:83)
      Caused by: java.net.ConnectException: Connection refused: connect
          at be.ac.umons.g04.api.core.resources.NotificationTest.test_getABunchOfNotifications(NotificationTest.java:83)

我尝试对资源进行不安全处理并使用 http://localhost:8080/api/notifications,但它输出了相同的错误

我尝试更换 @SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.MOCK) @TestPropertySource(properties = "server.port=8443") 仅包含 @SpringBoottest,但我得到相同的输出

我也尝试过直接模拟 Response,我模拟了 webTarget.request().get() 方法输出相同。

我不明白我想念什么。

我愿意接受您可能提出的任何建议/提示/建议。如果需要,请随时询问更多详细信息。 谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)