Closed shivakumarkn closed 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 :
@Mock
your ClientFactoryIf your ClientFactory instance should only be accessed in your OnModuleLoad, you could set the getInstance method as package protected.
Is that working for you ? Can I close the topic ?
Yes please.
Great :)
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{
}
MyViewImplTest{
}
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?