1

我决定回到我将 Kafka Metrics 与 Spring Boot Actuator 集成的想法,我在这里提到过:

https://github.com/spring-projects/spring-boot/issues/6227

到目前为止,我有一个带有工作代码的单独“沙盒”项目,我想将其合并到 Spring Boot 中。现在我有点困惑。我的部分测试需要 powermock 来模拟 Kafka 的“超级安全”类:

package org.apache.kafka.common.metrics;

// some imports...

public final class KafkaMetric implements Metric {
    private MetricName metricName;
    private final Object lock;
    private final Time time;
    private final Measurable measurable;
    private MetricConfig config;

    KafkaMetric(Object lock, MetricName metricName, Measurable measurable, MetricConfig config, Time time) {
        this.metricName = metricName;
        this.lock = lock;
        this.measurable = measurable;
        this.config = config;
        this.time = time;
    }

    // some good code here, but no public constructor and the class is final... of course.

}

但在 Spring Boot 中不使用 powermock。

我应该怎么办?

  1. 添加最新的稳定 powermock - 在 spring-boot-dependencies 和 spring-boot-actuator 中。
  2. 仅将最新的稳定 powermock 添加到 spring-boot-actuator - 让它成为一个隐藏的秘密助手。
  3. 排除需要 powermock 的测试。
  4. 忘记 Kafka Metrics,它们又大又可怕,没有人希望它们出现在我们漂亮友好的 Spring Boot Actuator 中。
4

1 回答 1

1

在评论中得到您的确认后,我只是在此处复制我的评论作为正确关闭问题的有效答案。

您可以尝试使用Metrics.addMetric()来创建KafkaMetric实例,然后通过您的 对它做出反应KafkaStatisticsProvider.metricChange(),您将Metrics在测试之前在实例中注册。当然,而不是 Powermock。

我将在下周找时间为您的执行器项目做出贡献,以避免 Powermock。

于 2016-12-03T05:13:50.630 回答