2017-09-01 129 views
1

我有一個指標收集器,它將數據存儲在InfluxDB上,我想測試存儲這些指標的方法。我正在嘗試,但我無法嘲笑InfluxDB客戶端。我不想在測試環境中指出一個真實的InfluxDB模擬InfluxDB客戶端測試MetricCollector類

到目前爲止我所取得的一切都是一些「空指針異常」,並且連接被拒絕。

這是我的測試(使用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(); 
      } 
     } 
    } 

回答

0

我完全誤解了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(); 
      } 
     } 
    } 

是的,補充說,簡單的「模擬」註釋和一切正常。

1

我懷疑你不能嘲笑InfluxDB客戶端的原因是,它是一個靜態方法創建:InfluxDBFactory.connect()。要嘲笑這你需要PowerMock

事情是這樣的:

@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兼容性要求描述herehere

+0

你的代碼對我不起作用,但你讓我以正確的方式。我將編輯問題並添加解決方案。謝謝 – FranAguiar