【问题标题】:How to Mock a Declarative Client in Micronaut?如何在 Micronaut 中模拟声明式客户端?
【发布时间】:2021-09-14 10:02:29
【问题描述】:

我有一个类,我在其中调用一个声明式客户端的方法。但是对于我的测试,我不希望它调用实际的 URL。相反,我想模拟它。我该怎么做,因为它不是一个类,而是一个用 @Client 注释的接口?

示例代码:- here。请查看第 4.3 节

【问题讨论】:

    标签: client httpclient micronaut micronaut-rest micronaut-test


    【解决方案1】:

    在您的测试中,您可以用模拟替换 http 客户端 bean。请在下方找到 Spock sn-p。

    // replace the client with a mock
    @MockBean(YourClientInterface)
    YourClientInterface yourClientInterface() {
        return Mock(YourClientInterface)
    }
    
    // inject the mock in order to configure responses when it gets called
    @Inject
    YourClientInterface client
    
    

    现在您可以编写测试,并且您的代码将针对模拟而不是实际的 http 客户端运行。

    【讨论】:

    • 这在 Micronaut 2.x 中似乎不再适用
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多