|
|||||||
![]() |
|
|
Thread Tools |
|
#1
|
|||
|
|||
|
I'm using GXT's MVC framework in my project and I'd like to write JUnit tests for my controllers. It appears this isn't possible and I have to write tests that extend GWTTestCase.
Here's a test I have written: public class LoginControllerTest extends TestCase {
public void testCreateView() {
LoginController loginController = new LoginController();
Dispatcher dispatcher = Dispatcher.get();
dispatcher.addController(loginController);
dispatcher.dispatch(AppEvents.Login);
assertNotNull(loginController.loginView);
}
}
java.lang.ExceptionInInitializerError at com.extjs.gxt.ui.client.mvc.Dispatcher.<init>(Dispatcher.java:124) at com.extjs.gxt.ui.client.mvc.Dispatcher.get(Dispatcher.java:111) If I have to extend GWTTestCase, are there any plans to add pure JUnit support in a future release? Thanks, Matt |
|
#2
|
|||
|
|||
|
Yes, I had the same problem.
My workaround is creating my own Dispatcher for testing which strips out any history support (which causes Dispatcher.get() to fail). This works but it is a bit ugly. |
|
#3
|
|||
|
|||
|
Hi,
I'm not sure to see the point of testing controllers with pure junit. Controllers are meant to be used in a GWT application (a javascript application) and not in pure java. IHMO, it is useless and you should just extend GWTTestCase. Do I miss something ? |
|
#4
|
|||
|
|||
|
Ok, I've just read your blog, now I understand the need, except that with "pure" JUnit, you would test your code in a JVM whereas your application will execute it in a Javascript context.
|
|
#5
|
|||
|
|||
|
As returnity said, the problem is that Dispatcher depends on History, which is a GWT class using JSNI.
|
|
#6
|
|||
|
|||
|
I tried to use GWTMockUtilities to get around the history issue, but that doesn't seem to work either.
http://extjs.com/forum/showthread.php?t=60197 |
|
#7
|
|||
|
|||
|
Well, I guess the only solution you have is to create your own Dispatcher class for MVC tests (without GWT History support). The following should work (put it in the package com.extjs.gxt.ui.client.mvc in your test classes).
package com.extjs.gxt.ui.client.mvc;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.extjs.gxt.ui.client.event.BaseObservable;
import com.extjs.gxt.ui.client.event.MvcEvent;
public class Dispatcher extends BaseObservable {
public static final int BeforeDispatch = 955;
public static final int AfterDispatch = 960;
private static Dispatcher instance;
public static void forwardEvent(AppEvent event) {
instance.dispatch(event);
}
public static void forwardEvent(int eventType) {
instance.dispatch(eventType);
}
public static void forwardEvent(int eventType, Object data) {
instance.dispatch(new AppEvent(eventType, data));
}
public static void forwardEvent(int eventType, Object data, boolean historyEvent) {
AppEvent ae = new AppEvent(eventType, data);
ae.historyEvent = historyEvent;
instance.dispatch(ae);
}
public static Dispatcher get() {
if (instance == null) {
instance = new Dispatcher();
}
return instance;
}
private Map<String, AppEvent> history;
private List<Controller> controllers;
private Dispatcher() {
instance = this;
controllers = new ArrayList<Controller>();
history = new HashMap<String, AppEvent>();
}
public void addController(Controller controller) {
if (!controllers.contains(controller)) {
controllers.add(controller);
}
}
public void addDispatcherListener(DispatcherListener listener) {
addListener(BeforeDispatch, listener);
addListener(AfterDispatch, listener);
}
public void dispatch(AppEvent event) {
dispatch(event, true);
}
public void dispatch(int type) {
dispatch(new AppEvent(type));
}
public void dispatch(int type, Object data) {
dispatch(new AppEvent(type, data));
}
public List<Controller> getControllers() {
return controllers;
}
public Map<String, AppEvent> getHistory() {
return history;
}
public void removeController(Controller controller) {
controllers.remove(controller);
}
public void removeDispatcherListener(DispatcherListener listener) {
removeListener(BeforeDispatch, listener);
removeListener(AfterDispatch, listener);
}
private void dispatch(AppEvent event, boolean createhistory) {
MvcEvent e = new MvcEvent(this, event);
e.source = this;
e.appEvent = event;
if (fireEvent(BeforeDispatch, e)) {
for (Controller controller : controllers) {
if (controller.canHandle(event)) {
if (!controller.initialized) {
controller.initialized = true;
controller.initialize();
}
controller.handleEvent(event);
}
}
fireEvent(AfterDispatch, e);
}
if (createhistory && event.historyEvent) {
String token = event.token;
if (token == null) {
token = "" + new Date().getTime();
}
history.put(token, event);
}
}
}
|
|
#8
|
|||
|
|||
|
Quote:
Thanks, Matt [1] http://code.google.com/p/gwt-maven/ |
![]() |
| Thread Tools | |
|
|