user1039063
user1039063

Reputation: 211

Injecting into a static method/field with easymock?

I know that one of the rules for the easy mock is that it can not inject into static and final fields.

However,

if I have a code like:

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

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

So, without re-writing the code itself, can I write unit test for such class? Is there a way I can mock the Customer and inject it into startSomeWork()?

Using EasyMock and jUnit.

Thank you

Upvotes: 0

Views: 597

Answers (1)

Vihar
Vihar

Reputation: 3831

You can do something like this

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

worked for me

Good luck!

Upvotes: 1

Related Questions