有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java PowerMockito返回错误的对象

我在测试中使用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 );

我为所有需要的类型创建了一些模拟语句。(如您所见,对于一种类型,需要模拟的方法不止一个)

问题是:PowerMockito总是返回上一次PowerMockito中设置的对象。当(…)声明


共 (1) 个答案

  1. # 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
    • 尽可能避免使用静态类,正如您所看到的,测试更加困难;)

    编辑

    看看这个例子,它与您当前的用例类似:

    类别:

    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));
    }