2017-08-15 54 views
0

我被這個問題困住了整整一天。嘗試了許多資源和調試功能,但沒有運氣。通緝但未啓用:與此模擬之間沒有任何互動

你們能幫我嗎?

public class Data{ 

    DataDriver dataDriver; // DataDriver is a different class which I have injected. 
    Connector connector; // Connector is a different class which I am injecting in Data class. 

    // Constructor and Setter methods 

    public void read(){ 
      CloseableHttpClient client = HttpClients.createDefault(); 
      Hashmap<String, String> credentials = dataDriver.run(new Date()); 
      HttpGet get = connector.execute(client,credentials); 
    } 
} 

對於我的測試類,我使用Powermockito,作爲HttClients.createDefault()是一個靜態方法。

@ContextConfiguration(locations = { "classpath:/XXX.xml" }) 
@RunWith(PowerMockRunner.class) 
public class DataTest { 

    Data mockeddata; 

    @Mock 
    Connector mockedconnector; 

    @Mock 
    DataDriver mockeddataDriver; 

    @Mock 
    CloseableHttpClient mockedClient; 

    @Before 
    public void Setup() { 
     MockitoAnnotations.initMocks(Data.class); 
     mockeddata =new Data(); 
     PowerMockito.mockStatic(HttpClients.class); 
     mockedData.setConnector(mockedconnector); 
     mockedData.setDataDriver(mockedDataDriver); 
    } 

    @Test 
    @PrepareForTest({ HttpClients.class }) 
    public void testRead(){ 
     HashMap<String, String> credentials = new HashMap<String, String>();  
     PowerMockito.when(HttpClients.createDefault()).thenReturn(mockedClient); 
     PowerMockito.when(mockeddataDriver.run(newDate())).thenReturn(credentials); 
     PowerMockito.when(mockedconnector.execute(mockedClient,credentials)).thenReturn(new HttpGet()); 
     verify(mockedconnector, atLeastOnce()).execute(mockedClient, credentials); 
    } 
} 

它說與連接器模擬的零交互。它不抱怨dataDriver模擬。我試過調試,並能夠在Data類中看到我的模擬對象。但它不會進入連接器的功能執行功能。

任何意見我做錯了什麼?

謝謝

回答

0

也許當你確認是的Mockito創建不同的對象引用(不同的對象)的參數... 嘗試用eq()方法驗證,以確保您所期望的參數是一樣的when你已經定義過。

verify(mockedconnector,atLeastOnce()).execute(eq(mockedClient),eq(credentials));

0

你是不是從你的測試類調用您的read方法。

相關問題