Jmockit mock abstract class

自古美人都是妖i 提交于 2019-12-11 02:06:54

问题


I have a super abstract class

public abstract class PsActionBeanContext{
    ...
    abstract public Brand getBrand();
    ..
}

I want to mock the abstract class to get default value "TESTUSER_BRAND_ID", which is a constant. I tried:

final PsActionBeanContext contextFake = new MockUp<PsActionBeanContext>(){
 @Mock
 public Brand getBrand(){
      Brand brand = new Brand();
      brand.setBrandId(TESTUSER_BRAND_ID);
      return brand;
      }
 }.getMockInstance();
}

But I got

java.lang.IllegalArgumentException: Attempted to mock abstract method "getBrand"

I tried the same way to mock an interface but that is fine.
any suggestion? thanks


回答1:


Try the following instead:

@Test
public void mockAbstractClass(@NonStrict final PsActionBeanContext mock)
{
    final Brand brand = new Brand();
    brand.setBrandId(TESTUSER_BRAND_ID);

    new Expectations() {{ mock.getBrand(); result = brand; }};

    assertSame(brand, mock.getBrand());
}


来源:https://stackoverflow.com/questions/16862032/jmockit-mock-abstract-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!