首页 > 解决方案 > RestEasy JUnit Test:测试外部 API 是否正在使用 JUnit 测试

问题描述

我在 J 单元测试中完全是新手,所以我到目前为止:

我使用 RESTEasy 在我的应用程序中调用外部 API,如下所示:

    @Path("path/to")
    @RegisterRestClient(configKey = "external-api")
    @OidcClientFilter
    public interface DummyService {
    
        @POST
        @Path("/endpoint")
        @Produces(MediaType.APPLICATION_JSON)
        MyResponseObj getApple(Apple apple);

    }

@OidcClientFilter 配置为从授权服务器获取承载令牌,并将其放入 DummyService 中的每个请求标头中。这项服务运作良好。现在我想为此服务编写一个 JUnit 测试来测试 Rest Call。如果有人能在正确的方向上为我提供建议,我将不胜感激。

我的想法是写这样的东西,但这不起作用:

public class DummyServiceTest {

    @Inject
    @RestClient
    DummyService dummyService;

    @Test
    public void testDummyServiceCall() {
        Apple apple = new Apple();
        apple.setColor("red");
        MyResponseObj myResponseObj = dummyService.getApple(apple);
        assertFalse(myResponseObj.isEmpty());
    }
}

我不想模拟 DummyService,我希望测试对外部服务器上的 API 执行真实请求。

如何编写测试这种休息呼叫的测试?

标签: javajunitresteasyoidc-client

解决方案


推荐阅读