Java 模拟XDB客户端以测试MetricCollector类
我有一个度量采集器,它在XDB上存储数据,我想测试存储该度量的方法。我正在尝试,但无法模拟XDB客户端。我不想指出测试环境中真正的Java 模拟XDB客户端以测试MetricCollector类,java,mockito,testng,influxdb,Java,Mockito,Testng,Influxdb,我有一个度量采集器,它在XDB上存储数据,我想测试存储该度量的方法。我正在尝试,但无法模拟XDB客户端。我不想指出测试环境中真正的InfluxDB 到目前为止,我所取得的一切都是一些“空指针异常”和连接被拒绝 这是我的测试(使用TestNG)。我做错了什么 @Test public void validateMetrics() { String influxHost = "http://localhost"; String credentials
InfluxDB
到目前为止,我所取得的一切都是一些“空指针异常”和连接被拒绝
这是我的测试(使用TestNG)。我做错了什么
@Test
public void validateMetrics() {
String influxHost = "http://localhost";
String credentials = "admin:admin";
String influxDatabaseName = "testDataBase";
influxDB = InfluxDBFactory.connect(influxHost, credentials.split(":")[0], credentials.split(":")[1]);
MetricsCollector metricsCollector = null;
try {
String hostName = "test-server-01";
int statusValue = 1;
metricsCollector = new MetricsCollector(influxDB);
BatchPoints metrics = metricsCollector.initBatchPoint(influxDatabaseName);
Point point = metricsCollector.setMetric(hostName, "status", statusValue);
metrics = metricsCollector.addToMetrics(metrics, point);
Assert.assertTrue(metrics.getPoints().get(0).lineProtocol().contains(hostName));
Assert.assertTrue(metrics.getPoints().get(0).lineProtocol().contains("status=" + statusValue));
} finally {
if (metricsCollector != null) {
metricsCollector.closeConnection();
}
}
}
我怀疑您无法模拟InfluxDB客户端的原因是它是由静态方法创建的:
InfluxDBFactory.connect()
。要嘲笑这一点,您需要
大概是这样的:
@PrepareForTest({InfluxDBFactory.class})
public class ATestClass {
@Test
public void validateMetrics() {
// this allows you to mock static methods on InfluxDBFactory
PowerMockito.mockStatic(InfluxDBFactory.class);
String influxHost = "http://localhost";
String credentials = "admin:admin";
String influxDatabaseName = "testDataBase";
InfluxDB influxDB = Mockito.mock(InfluxDB.class);
// when the connect() method is invoked in your test run it will return a mocked InfluxDB rather than a _real_ InfluxDB
PowerMockito.when(InfluxDBFactory.connect(influxHost, credentials.split(":")[0], credentials.split(":")[1])).thenReturn(influxDB);
// you won't do this in your test, I've only included it here to show you that InfluxDBFactory.connect() returns the mocked instance of InfluxDB
InfluxDB actual = InfluxDBFactory.connect(influxHost, credentials.split(":")[0], credentials.split(":")[1]);
Assert.assertSame(influxDB, actual);
// the rest of your test
// ...
}
}
注意:TestNG、Mockito和PowerMock都有特定的兼容性要求,我完全误解了Mockito的工作原理。这是我的固定代码:
@Mock private InfluxDB influxDB;
@Test
public void validateMetrics() {
MetricsCollector metricsCollector = null;
String influxHost = "http://localhost";
String credentials = "admin:admin";
String influxDatabaseName = "testDataBase";
influxDB = InfluxDBFactory.connect(influxHost, credentials.split(":")[0], credentials.split(":")[1]);
try {
String hostName = "test-server-01";
int statusValue = 1;
metricsCollector = new MetricsCollector(influxDB);
BatchPoints metrics = metricsCollector.initBatchPoint(influxDatabaseName);
Point point = metricsCollector.setMetric(hostName, "status", statusValue);
metrics = metricsCollector.addToMetrics(metrics, point);
Assert.assertTrue(metrics.getPoints().get(0).lineProtocol().contains(hostName));
Assert.assertTrue(metrics.getPoints().get(0).lineProtocol().contains("status=" + statusValue));
} finally {
if (metricsCollector != null) {
metricsCollector.closeConnection();
}
}
}
是的,添加了简单的“mock”注释,一切正常。您的代码对我不起作用,但您用了正确的方法。我将编辑问题并添加解决方案。谢谢