Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: Mocks, testing and GXT philosophy

  1. #21
    Sencha Premium Member
    Join Date
    Sep 2007
    Posts
    13,976

    Default

    As i already said, GXT3 will contain the required changes. http://www.extjs.com/products/gwt/roadmap.php

  2. #22
    Ext User
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    12

    Default

    The GWT event handlers and interfaces on the GXT roadmap are great news!

  3. #23
    Ext GWT Premium Member jorel2's Avatar
    Join Date
    Apr 2010
    Location
    Durham, NC
    Posts
    161

    Default

    Quote Originally Posted by Ozzy View Post
    I've run into the same problem trying to mock out portions of my application for testing. ...
    What I did was to add adapters around GXT widgets so that I could turn a ButtonEvent into a ClickEvent. The GXT button adapter implements HasClickHandlers, and a user of the apapter simply registers a ClickHandler and process the ClickEvent(s) generated. I've adapted other GXT widget classes and added my own events in some places (as opposed to using GWT events).

    This model works well, expect for the fact that I have to write all the adapters .
    -- Patrick.
    Ozzy,

    Would you be interested in posting your adapters for others to use?

    regards,
    Jorel

  4. #24
    Ext User
    Join Date
    Jan 2010
    Location
    ireland
    Posts
    1

    Default GXT Mocking

    We finally managed to go some way towards keeping Test driven development with JMock and GXT 2.1 by making a base class for our test classes which would use javaassist to disarm GXT. Extract as follows : (still had to use an interface approach for a few components, particulary GXT combobox could not make a mock one with the static design, etc., as mentioned in thread, but this got us passed alot)

    ------------------------------
    import org.jmock.Expectations;
    import org.jmock.Mockery;
    import org.jmock.integration.junit4.JUnit4Mockery;
    import org.jmock.lib.legacy.ClassImposteriser;
    import org.junit.BeforeClass;

    import com.google.gwt.junit.GWTMockUtilities;

    public abstract class BaseTestUI {

    private static boolean isDisarmed;

    protected Mockery context = new JUnit4Mockery();

    {
    context.setImposteriser(ClassImposteriser.INSTANCE);
    }

    @BeforeClass
    public static void setup() {

    if (!isDisarmed) {
    GXTMockUtilities.disarm(); // our own class
    GWTMockUtilities.disarm();
    isDisarmed = true;
    }

    }

    }
    ------------------------------

    Where GXTMockUtilities is as follows:


    import javassist.CannotCompileException;
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.CtNewMethod;
    import javassist.Modifier;
    import javassist.NotFoundException;

    public class GXTMockUtilities {
    private static boolean alreadyDisarmed;


    public static void disarm() {

    if (!alreadyDisarmed) {
    try {

    final CtClass gwtClazz = ClassPool.getDefault().get("com.google.gwt.core.client.impl.Impl");

    final CtClass gxtClazz = ClassPool.getDefault().get("com.extjs.gxt.ui.client.GXT");

    final CtClass returnClazz = ClassPool.getDefault().get("java.lang.String");

    renameAndReplaceMethod(gwtClazz, returnClazz, "getModuleBaseURL");
    // renameAndReplaceMethod(gwtClazz, returnClazz,
    // "getHostPageBaseURL");
    renameAndReplaceMethod(gxtClazz, CtClass.voidType, "init");
    // Load the classes in the classloader
    gwtClazz.toClass();
    gxtClazz.toClass();
    alreadyDisarmed = true;

    } catch (final NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (final CannotCompileException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

    // this is somewhat dangerous as you will be reloading classes
    // the JVM doesn't really like this
    public static void restore() {

    }

    private static void renameAndReplaceMethod(final CtClass clazz, final CtClass returnClazz, final String methodName)
    throws NotFoundException, CannotCompileException {

    final CtMethod oldMethod = clazz.getDeclaredMethod(methodName);

    // rename old method to synthetic name
    final String dummyMethodName = methodName + "$impl";
    oldMethod.setName(dummyMethodName);

    // create a new dummy method
    // can't simply copy as the methods are native
    final StringBuilder body = new StringBuilder();
    body.append("{");
    body.append("return \"\";");
    body.append("}");

    final CtMethod newMethod = CtNewMethod.make(Modifier.STATIC | Modifier.PUBLIC, returnClazz, methodName,
    new CtClass[] {}, new CtClass[] {}, body.toString(), clazz);

    clazz.addMethod(newMethod);

    }

    }
    Last edited by corm; 4 Jul 2010 at 5:25 AM. Reason: StringBuilder not StringBuffer

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •