Java Mockito如何为属性设置新值

时间:2016-03-21 16:06:29

标签: java mockito

我有Filter模拟对象并将其传递给测试方法。它方法更改Filter元属性值。但在我的测试中,我得到了旧的价值。

  

org.junit.ComparisonFailure:expected:item [28140f6f5a]但是:item [3abf441213]       在org.junit.Assert.assertEquals(Assert.java:115)       ...

@Test
public void testMetaValue() throws Exception{
    when(f.getMetaData("last")).thenReturn("item3abf441213");

    WebUK at = new WebUK();
    at.setCollectingService(new TestCollectingServiceImpl());

    Set<Ad> ads = at.getNewAds(f);
    assertEquals(1, ads.size());
    assertEquals("item28140f6f5a",f.getMetaData("last"));
} 

测试对象:

public Set<Ad> getNewAds(Filter filter) throws Exception {
        ...
        filter.setMetaData("last", ul.get(0).attr("id"));
        ...
}

1 个答案:

答案 0 :(得分:1)

您可以通过验证检查是否调用了set方法:

verify(f).setMetaData("last", "item28140f6f5a");