2015-12-14 35 views
1

我有一個程序,我想模擬使用Mockito的數據。
我的主要計劃是AdDataAggregate,它叫AdDataConnect
AdDataConnect調用一個API,該API返回一個被解析爲JSON並聚合的字符串。我的模擬沒有被我的程序使用

我創建了模擬並返回數據,但它們並未被主程序使用。
有人可以告訴我我的方式錯誤。在此先感謝您的幫助。

//Main Program 
public class AdDataAggregate {
 
public List<AdData> processInfo(long[] adIds){
   
    AdDataConnect adDataConnect = new AdDataConnect();
   
    List<AdData> adAccumData = new ArrayList<AdData>();    

    for (long adId: adIds) {  
     data = adDataConnect.connectToData(adId); <———— method to Mock. 
     } 
  } 
}
  


//Test Program 
@RunWith(MockitoJUnitRunner.class)
  
public class AdDataTest {
  
   
@InjectMocks    
    private AdDataAggregate adDataAggregate = new AdDataAggregate();
   
    @Mock 
    private AdDataConnect adDataConnect;
    
    private String oneAdId = 
      "[{\"advertiser_id\":\"1\",\"ymd\":\"2015-12-" + 
      "07\",\"num_clicks\":10,\"num_impressions\":100}," +
          
      "{\"advertiser_id\":\"1\",\"ymd\":\"2015-12-" + 
      "06\",\"num_clicks\":20,\"num_impressions\":200}," +
  
      "{\"advertiser_id\":\"1\",\"ymd\":\"2015-12-" + 
      "05\",\"num_clicks\":30,\"num_impressions\":300}]";
 

    
 @Test  
    public void testWithOneAdId(){
   
     MockitoAnnotations.initMocks(this); 
     adDataConnect = mock(AdDataConnect.class); 
     adDataAggregate = mock(AdDataAggregate.class); 
     when(adDataConnect.connectToData(eq(1L)))
.thenReturn(oneAdId);  
     String myString = adDataConnect.connectToData(1L);
    
     long[] adIds = new long[]{1L};
    
     List<AdData> outData = adDataAggregate.processInfo(adIds);
 
     Assert.assertEquals(1, outData.size());
    
     Assert.assertEquals(myString, oneAdId);
 
     Assert.assertEquals(60, outData.get(0).getNumImpressions());
    
     Assert.assertEquals(600, outData.get(0).getNumImpressions());
    
    }
   
} 

回答

1

您需要將AdDataConnect注入AdDataAggregate而不是實例化它。當你在AdDataAggregate中實例化它時,你迫使一個實現被使用。您需要允許外部人員進行設置,以便您可以在測試中設置模擬版本。

下面是你是如何做到的。通過構造函數中的AdDataConnect:

public class AdDataAggregate 
{ 
    private AdDataConnect adDataConnect; 

    public AdDataAggregate(AdDataConnect adDataConnect) 
    { 
     this.adDataConnect = adDataConnect; 
    } 

    public List<AdData> processInfo(long[] adIds) 
    { 
     List<AdData> adAccumData = new ArrayList<AdData>(); 
     for(long adId : adIds) 
     { 
      data = adDataConnect.connectToData(adId); 
     } 
    } 
} 

然後在您的測試中執行此操作。你可以在我使用新構造函數的地方看到我的評論:

@RunWith(MockitoJUnitRunner.class) 
public class AdDataTest 
{ 
    private AdDataAggregate adDataAggregate; 

    @Mock 
    private AdDataConnect adDataConnect; 

    private String oneAdId = "[{\"advertiser_id\":\"1\",\"ymd\":\"2015-12-" 
      + "07\",\"num_clicks\":10,\"num_impressions\":100}," + "{\"advertiser_id\":\"1\",\"ymd\":\"2015-12-" 
      + "06\",\"num_clicks\":20,\"num_impressions\":200}," + "{\"advertiser_id\":\"1\",\"ymd\":\"2015-12-" 
      + "05\",\"num_clicks\":30,\"num_impressions\":300}]"; 

    @Test 
    public void testWithOneAdId() 
    { 
     MockitoAnnotations.initMocks(this); 
     adDataAggregate = new AdDataAggregate(adDataConnect);//PASS THE MOCK HERE 

     when(adDataConnect.connectToData(eq(1L))).thenReturn(oneAdId); 
     String myString = adDataConnect.connectToData(1L); 
     long[] adIds = new long[] { 1L }; 
     List<AdData> outData = adDataAggregate.processInfo(adIds); 
     Assert.assertEquals(1, outData.size()); 
     Assert.assertEquals(myString, oneAdId); 
     Assert.assertEquals(60, outData.get(0).getNumImpressions()); 
     Assert.assertEquals(600, outData.get(0).getNumImpressions()); 
    } 
} 
+0

非常感謝。這工作完美。 – user2547177

+0

太棒了!請接受我的回答,以便獲得信用。謝謝! –

相關問題