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() 方法但输出相同。

我不明白我想念什么。

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

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

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2162.html

大家都在问