我的程序没有使用我的模拟

时间:2015-12-14 02:30:06

标签: java unit-testing mockito

我有一个程序,我想模拟使用Mockito的数据 我的主要计划是AdDataAggregate,并且会调用AdDataConnect AdDataConnect调用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 个答案:

答案 0 :(得分: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());
    }
}