0

I've a problem with GWT unit test, this is the code

    StudenteServiceAsync service;

    public void gwtSetUp(){
        // Create the service that we will test.
        service = GWT.create(StudenteService.class);
        ServiceDefTarget target = (ServiceDefTarget) service;
        target.setServiceEntryPoint(GWT.getModuleBaseURL() + "progettosweng/studenti");
    }

    @Override
    public String getModuleName() {
        return "com.university";
    }

    @Test
    public synchronized void getNumeroStudenti() throws Exception {
        delayTestFinish(10000);
        service.getNumeroStudenti(new AsyncCallback<Integer>() {
            @Override
            public void onFailure(Throwable caught) {
                fail("FAIL: getNumeroStudenti");
            }

            @Override
            public void onSuccess(Integer result) {
                assertTrue(true);
            }
        });
    }
} 

when start test, it gives me an error:

java.lang.NullPointerException: Cannot invoke "com.university.client.services.StudenteServiceAsync.getNumeroStudenti(com.google.gwt.user.client.rpc.AsyncCallback)" because "this.service" is null

For me the error is never go in gwtSetUp() and the service is ever null. what should I do? thanks

Mr Alsi
  • 37
  • 6
  • It doen't look as though you call `gwtSetUp()` – tgdavies Nov 15 '22 at 08:39
  • where should call? – Mr Alsi Nov 15 '22 at 14:54
  • You could make `service` a local variable in `getNumeroStudenti` and call `gwtSetUp()` in `getNumeroStudenti`. Or if you have other tests which require the same service, you could annotate `gwtSetUp()` with `@BeforeEach`. – tgdavies Nov 15 '22 at 21:52
  • @tgdavies if I do it, give me an error: `java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code! It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.` – Mr Alsi Nov 21 '22 at 10:02
  • 1
    That error message explains the solution. – tgdavies Nov 21 '22 at 10:59

0 Answers0