Ext


Go Back   Ext JS Forums > Ext GWT Community Forums (1.x) > Gxt: Help (1.x)

Reply
 
Thread Tools
  #1  
Old 02-02-2009, 03:15 PM
mraible mraible is offline
Ext GWT Premium Member
 
Join Date: Nov 2007
Posts: 37
mraible is on a distinguished road
Default Is it possible to test Controllers with JUnit?

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);
    }
}
When I run this test, I get the following error:

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)
Do I have to write tests that extend GWTTestCase or is it possible to test GXT controllers and views with pure JUnit?

If I have to extend GWTTestCase, are there any plans to add pure JUnit support in a future release?

Thanks,

Matt
Reply With Quote
  #2  
Old 02-10-2009, 07:19 PM
returnity returnity is offline
Ext User
 
Join Date: Jan 2009
Posts: 1
returnity is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 02-11-2009, 05:14 AM
sdc sdc is offline
Ext User
 
Join Date: Jun 2008
Posts: 365
sdc is on a distinguished road
Default

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 ?
Reply With Quote
  #4  
Old 02-11-2009, 10:35 AM
sdc sdc is offline
Ext User
 
Join Date: Jun 2008
Posts: 365
sdc is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 02-16-2009, 05:21 AM
sdc sdc is offline
Ext User
 
Join Date: Jun 2008
Posts: 365
sdc is on a distinguished road
Default

As returnity said, the problem is that Dispatcher depends on History, which is a GWT class using JSNI.
Reply With Quote
  #6  
Old 02-17-2009, 09:33 AM
mraible mraible is offline
Ext GWT Premium Member
 
Join Date: Nov 2007
Posts: 37
mraible is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 02-17-2009, 09:58 AM
sdc sdc is offline
Ext User
 
Join Date: Jun 2008
Posts: 365
sdc is on a distinguished road
Default

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);
    }
  }

}
Reply With Quote
  #8  
Old 02-17-2009, 01:02 PM
mraible mraible is offline
Ext GWT Premium Member
 
Join Date: Nov 2007
Posts: 37
mraible is on a distinguished road
Default

Quote:
Originally Posted by sdc View Post
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).
Thanks. I tried this and put it in src/test/java/com/extjs/gxt/ui/client/mvc/Dispatcher.java. I'm using the gwt-maven-plugin[1] and it still seems to find the one that's packaged in the JAR first. I've tried running my test with both IDEA and from the command line.

Thanks,

Matt

[1] http://code.google.com/p/gwt-maven/
Reply With Quote
Reply

Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -5. The time now is 10:38 PM.

© 2006-2009 Ext, LLC
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.