gwt-test-utils / gwt-test-utils

gwt-test-utils is a Java framework that allows to test GWT client side code in a efficient, easy way
125 stars 49 forks source link

Does gwt-test-util test case invokes onModuleLoad? #3

Closed shivakumarkn closed 12 years ago

shivakumarkn commented 12 years ago

I am wondering whether running a test case written using gwt-test-utils parses *.gwt.xml and go through onModuleLoad() method.

This is because, I have a factory class and its implementation which creates all views in my application.

This factory class is defined in *.gwt.xml as

The same factory class is deferred binding created in onModuleLoad() as public static ClientFactory clientFactory = null; public void onModuleLoad(){ clientFactory = GWT.create(ClientFactory.class); //Note that it is not Impl class. }

My view impl class uses this client factory to get the other views created.

MyViewImpl{

public void refreshView()[
    MySecondView  mySecondView = OnModuleLoadClass.clientFactory.getMySecondView();  //This should return MySecondViewImpl.
}

}

MyViewImplTest{

MyViewImpl myViewImpl

@before
public void init()[
     myViewImpl = new MyViewImpl();
     myViewImpl.refreshView();  //This is throwing NullPointerException as clientFactory in refreshView is coming null.
}

}

I initially thought of Mocking the ClientFactory. But mocking didn't help because client factory is not set in the ViewImpl but accessed statically from OnModuleLoad class.

Is it a solvable issue within test class or a design smell?

Gael commented 12 years ago

I think having your ClientFactory instance in your OnModuleLoadClass is effectively not the better way to do it. You have to instanciate the OnModuleLoadClass in every test where you want to @Mock your ClientFactory instance.

What I would do instead is to implement ClientFactory a singleton like this :

public class ClientFactory {

   private static ClientFactory impl;

   public static ClientFactory getInstance() {
      if (impl == null) {
            impl = GWT.create(ClientFactory.class);
      }

      return impl;
   }

}

With that, in your test classes, you have 2 options :

If your ClientFactory instance should only be accessed in your OnModuleLoad, you could set the getInstance method as package protected.

Gael commented 12 years ago

Is that working for you ? Can I close the topic ?

shivakumarkn commented 12 years ago

Yes please.

Gael commented 12 years ago

Great :)