本文介绍了Jmockit模拟抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超级抽象类

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

我想模拟抽象类以获取默认值"TESTUSER_BRAND_ID",它是一个常量. 我试过了:

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

但我有

我尝试了相同的方法来模拟接口,但这很好.
有什么建议吗?谢谢

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

推荐答案

请尝试以下操作:

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

这篇关于Jmockit模拟抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:29