gwtmockito与requestbuilder

时间:2016-12-05 20:03:16

标签: java gwt gwtmockito

这是我班级的一部分,我想测试一下:

public class PrefPanel extends Composite {

  private static PrefPanelUiBinder uiBinder =  GWT.create(PrefPanelUiBinder.class);

  interface PrefPanelUiBinder extends UiBinder<Widget, PrefPanel> {}

  public PrefPanel(GlobalParams globalParams) {
    initWidget(uiBinder.createAndBindUi(this));

    String url = URL.encode(globalParams.getBaseUrl() + "book.html");
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
    try {
      Request response = builder.sendRequest(jsonString, new RequestCallback() {

      @Override
      public void onError(Request request, Throwable exception) {
        displayError("Error");
      }

      @Override
      public void onResponseReceived(Request request, Response response) {
        updateBookList(response.getText());
      }
    });
  } catch (RequestException e) {
    displayError("Error");
  }

以下是我的测试类的一部分:

@RunWith(GwtMockitoTestRunner.class)
public class PositionServiceTest {

@Mock RequestBuilder builder;
@Mock GlobalParams globalParams;
@Mock URL url;
private PrefPanel prefPanel;

@Before
public void setup() {
  GwtMockito.useProviderForType(RequestBuilder.class, new FakeProvider() {
    @Override
    public Object getFake(Class aclass) {
      return builder;
    }
  });

when(globalParams.getBaseUrl()).thenReturn("http://localhost/");
prefPanel = new PrefPanel(globalParams);
...

当我开始调试时,我收到一条错误消息: - 网址不能为空 - java.lang.IllegalArgumentException - 在com.google.gwt.http.client.StringValidator.throwlfEmptyOrNull(StringValidator.java:52) - ......

在我创建RequestBuilder(新的RequestBuilder)的行上发生错误。我不知道如何创建RequestBuilder的新实例。你能给我一个线索吗?

我听说gwtmockit无法处理构造函数。有没有办法避免新的RequestBuilder?我必须使用powermockito吗?

0 个答案:

没有答案