PowerMockito thenReturn返回错误的对象

时间:2014-07-14 15:19:57

标签: java unit-testing mockito powermock

我在测试中使用PowerMockito来模拟静态缓存。 通常,缓存的工作方式如下:

Cache.getInstance().findEntityById(AbstractDTO);
// so, if I want a TypeA, I use:
TypeADTo typeADTO = // go the dto from the db ...
TypeA = Cache.getInstance().findEntityById(typeADTO);

静态缓存在应用程序中广泛使用。所以要在单元测试中使用它,我使用:

    PowerMockito.mockStatic( Cache.class );
    final Cache mockedCache = PowerMockito.mock( Cache.class );
    PowerMockito.when( Cache.getInstance() ).thenReturn( mockedCache );
// mock all I want to get
  TypeA typeA = new TypeA(some parameters);
  TypeB typeB = new TypeB(some parameters);

PowerMockito.when(
        Cache.getInstance().findEntityByBusinessId(
                Mockito.any( TypeADTO.class ) ) ).thenReturn( typeA );
PowerMockito.when(
        Cache.getInstance().findEntityByBusinessId(
                Mockito.any( TypeADTO.class ), Mockito.anyBoolean() ) )
        .thenReturn( typeA );


PowerMockito.when(
        Cache.getInstance().findEntityByBusinessId(
                Mockito.any( TypeBDTO.class ) ) ).thenReturn(
        tybeB );
PowerMockito.when(
        Cache.getInstance().findEntityByBusinessId(
                Mockito.any( TypeBDTO.class ), Mockito.anyBoolean() ) )
        .thenReturn( typeB );

我为所有需要的类型创建了一些模拟语句。 (正如您所看到的,有一种方法需要针对一种类型进行模拟)

问题是:power mockito总是返回在最后一个PowerMockito.when(...)语句中设置的对象。

1 个答案:

答案 0 :(得分:1)

您是否尝试过连接电话?还可以在字时使用Mockito的

Mockito.when(Cache.getInstance().findEntityByBusinessId(any(TypeBDTO.class),anyBoolean()))
       .thenReturn( typeA )
       .thenReturn( typeB );

这将按照该顺序记录模拟。

以下参考的完整示例:

@RunWith(PowerMockRunner.class)
@PrepareForTest({BusinessUtility.class})
public class BusinessUtilityTest {

    @Before
    public void setUp() {
        PowerMockito.mockStatic(BusinessUtility.class);
    }

    @Test
    public void testStatic() {
        when(BusinessUtility.getDate())
                         .thenReturn(new Date(1111))
                         .thenReturn(new Date(2222));

        assertThat(BusinessUtility.getDate()).hasTime(1111);
        assertThat(BusinessUtility.getDate()).hasTime(2222);
    }
}

提示:

  • 使用静态导入使代码更具可读性
  • 使用PowerMockito初始化静态类,然后继续使用Mockito
  • 尽可能避免使用静态类,测试更难,如您所见;)

EDIT ----------------------------------------------

看看这个例子,类似于你当前的用例:

类别:

static class BusinessUtility {
    public static <T> T getObject(T instance) {
        return null;
    }

    public static <T> T getObject(T instance, Boolean b) {
        return null;
    }
}

测试:

@Test
public void testStatic() {
    //arrange
    when(BusinessUtility.getObject(anyString()))
            .thenReturn("one")
            .thenReturn("two");

    when(BusinessUtility.getObject(any(Date.class), anyBoolean()))
            .thenReturn(new Date(1111))
            .thenReturn(new Date(2222));

    //act
    String firstStr = BusinessUtility.getObject("asdf");
    String secondStr = BusinessUtility.getObject("qwerty");

    Date firstDate = BusinessUtility.getObject(new Date(), true);
    Date secondDate = BusinessUtility.getObject(new Date(), false);

    //assert
    assertThat(firstStr).isEqualTo("one");
    assertThat(secondStr).isEqualTo("two");

    assertThat(firstDate).isEqualTo(new Date(1111));
    assertThat(secondDate).isEqualTo(new Date(2222));
}