views:

14

answers:

1

hi,

I want to test certain underlying services using powermock. but it is complicated.

i would like to get your suggestion

public interface service{
  public void some_method(){
  }
}
public serviceclient implements service {
 ...
}
public myserviceclient {
 public service getService(){
  return service;
 }
}

I have written a serviceutil which uses myserviceclient to call and gets the services.

public class serviceutil{
 private static service s = myserviceclient.getService();
 public void updateservice(){
  // do some thing with service
 }
}

now i want to test serviceutil method - updateservice

how do i do it?

A: 

What you probably want is to inject a mock version of the service. This can be done like this:

service mockService = Mockito.mock(service.class);
Whitebox.setInternalState(serviceutil.class, mockService);

You can read more about bypassing encapsulation here: http://code.google.com/p/powermock/wiki/BypassEncapsulation

Jan Kronquist