2015-04-02 66 views
2

我使用Mockito對於具有以下條件的方法寫JUnit如何繞過/模擬動物園管理員連接

if(curatorFramework.getZooKeeperClient().isConnected() { 
    //do something 
} 

考試我一定要做點什麼的一部分。爲此,我試圖模擬IF條件爲真。下面我嘗試:

@Mock CuratorFramework curatorFrameworkMock 
when(curatorFrameworkMock.getZooKeeperClient().isConnected()).thenReturn(true); 

但它拋出NullPointerException因爲

curatorFrameworkMock.getZooKeeperClient() 
expects ZooKeeperClient Object to call isConnected().

沒有辦法,我可以在我的JUnit類中創建的ZooKeeperClient的對象。如何通過Mocking將此IF條件設置爲TRUE?

回答

2

只需創建一個模擬對象CuratorZookeeperClient並讓該對象模擬對isConnected的響應。

CuratorZookeeperClient curatorZookeeperClientMock = Mockito.mock(CuratorZookeeperClient.class); 
when(curatorZookeeperClientMock.isConnected()).thenReturn(true); 

CuratorFramework curatorFrameworkMock = Mockito.mock(CuratorFramework.class); 
when(curatorFrameworkMock.getZookeeperClient()).thenReturn(curatorZookeeperClientMock); 
+0

返回它我已經用同樣的方式嘗試過。它仍然在IF條件下拋出NullPointerException。 – 2015-04-07 06:12:44

+0

即使您將'curatorZookeeperClientMock'對象作爲模擬結果傳遞給'getZookeeperClient()'? 此設置適用於我使用Mockito版本1.10.8 – 2015-04-07 18:23:04

+0

是的。我已經試過這之前,你發佈這個答案,它會引發NullPointerException。順便說一句,我在Mockito 1.9.5。 如果您有任何問題,請分享帶有類似scenerio的JUnit代碼。 在此先感謝:) – 2015-04-08 04:28:51

0

這可能比您希望的多一點羅嗦,但這裏是我們正在使用的測試。模擬用於模擬策展人數據,我們希望檢索駱駝路線所需的卡夫卡服務器的IP地址。

@Test 
public void testURIExtraction() throws Exception 
{ 
    //Create a mock curator to return mock kafka IP data 
    ExistsBuilder existsBuilder = Mockito.mock(ExistsBuilder.class); 
    when(existsBuilder.forPath(KAFKA_BASE_PATH)).thenReturn(new Stat()); 

    GetDataBuilder getDataBuilder = Mockito.mock(GetDataBuilder.class); 
    when(getDataBuilder.forPath(KAFKA_BASE_PATH)).thenReturn(_createMockData()); 

    CuratorFramework curatorFramework = Mockito.mock(CuratorFramework.class); 

    when(curatorFramework.checkExists()).thenReturn(existsBuilder); 
    when(curatorFramework.getData()).thenReturn(getDataBuilder); 

    //create a kafkaURIExtractor object and generate the trafficeEventEndpointURI 
    KafkaURIExtractor kafkaURIExtractor = new KafkaURIExtractor(); 
    kafkaURIExtractor.setBasePath(KAFKA_BASE_PATH); 
    kafkaURIExtractor.setKafkaZKPort("2181"); 
    kafkaURIExtractor.setCurator(curatorFramework); 

    kafkaURIExtractor.init(); 
    String trafficEventEndpoint = kafkaURIExtractor.getTrafficEventEndpointURI(); 

    Assert.assertEquals(EXPECTED_RESULT, trafficEventEndpoint); 
} 

private byte[] _createMockData() throws JSONException 
{ 
    JSONArray data = new JSONArray(); 
    JSONObject location = new JSONObject(); 
    JSONArray ips = new JSONArray(); 
    ips.put("127.0.0.1"); 
    location.put("cluster", "test-mojave_dev"); 
    location.put("ips", ips); 
    data.put(location); 
    return data.toString().getBytes(); 
} 

它與您的示例類似,方法調用是嵌套的。 I.E.爲了調用curatorFramework.getData().forPath(...)首先要通過返回所期望的類型,在這種情況下是GetDataBuilder.class

KafkaURIExtractor對象簡單地檢查該KAFKA_BASE_PATH存在於館長然後檢索和格式化的數據的模擬對象地址curatorFramework.getData(),然後用getTrafiicEventEndpointURI()