使用 easymock 注入静态 method/field?

Injecting into a static method/field with easymock?

我知道 easy mock 的规则之一是它不能注入静态和最终字段。

然而,

如果我有这样的代码:

public final class SomeClass
{
  private static final AccessInternet accInternet = AccessInternetFactory.getimplmentation();

  public static void startSomeWork()
  {
    final Customer cust = new CurrentCustomerDetails().getCurrent();
    ...
  }
}

那么,在不重写代码本身的情况下,我可以为这样的class编写单元测试吗?有没有办法可以模拟客户并将其注入 startSomeWork()?

使用 EasyMock 和 jUnit。

谢谢

你可以这样做

Powermock.expectNew(Customer.class).andReturn(whateverSuitsYouAndCustomerClass).anyTimes();
Powermock.replayAll();

对我有用

祝你好运!