`
flashcloud
  • 浏览: 184890 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

MVP For GWT 系列资料转载四:Writing common test data services for gwt-dispatch with Guic

    博客分类:
  • GWT
阅读更多

源文转自:Writing common test data services for gwt-dispatch with Guic

 

The way it stands now after my last several posts on unit testing is that each test method in a JUnit TestCase runs its own context, including its own PersistenceManager injected with Guice. Because I’m initializing the AppEngine test environment with LocalDatastoreService.NO_STORAGE_PROPERTY = TRUE, any test data I create in one test is not available to the next test method, even in the same TestCase class. This is typical behavior for JUnit test cases, and generally a good thing as each test should be independent, but it does mean we need a convenient way to create test data. My current strategy is twofold:

 

Create test data used by all tests in the TestCase setUp() method that runs before each test

Create common test data (used by multiple TestCases) in test data generator services

The first problem to solve is how to get access to a dispatch service and PersistenceManager inside the test data generators. Both are now being injected by Guice as covered in previous posts, so I’ve written a BaseTestDataService that does the Guice magic:

 

package com.roa.test.service;

import net.customware.gwt.dispatch.client.standard.StandardDispatchService;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.roa.server.guice.ServerModule;
import com.turbomanage.gwt.server.PMF;
import com.turbomanage.gwt.server.guice.DispatchTestModule;

public class BaseTestDataService
{
	protected static Injector inj = Guice.createInjector(new ServerModule(),
		new DispatchTestModule());;

	protected static StandardDispatchService getService()
	{
		return inj.getInstance(StandardDispatchService.class);
	}

	protected static PMF getPMF()
	{
		return inj.getInstance(PMF.class);
	}
}

 

I’m intentionally using a static getPMF() method to make it as simple as possible to use the test data services in a unit test via static methods. Alternatively, I could have used instance methods and constructor injection, but then you’d have to create a new instance of a test data service in each test, which is just that much more code to write… Also, constructor injection is not possible in this case because the TestCases themselves are not instantiated by Guice, so instantiating a new test data service from a test case would not involve Guice, either.

 

It does not matter that the BaseTestDataService and BaseTest (below) are both calling Guice.createInjector() and thereby creating multiple Guice contexts, each having its own PMF instance. The important thing for these database tests is that they’re all going against one Datastore, not one PersistenceManager.

 

Here’s a simple test data generator that extends BaseTestDataService and provides a method to add a test user:

 

package com.roa.test.service;

import com.roa.client.domain.User;
import com.roa.shared.rpc.AddUserAction;
import com.roa.shared.rpc.AddUserResult;

public class UserTestDataService extends BaseTestDataService
{
	public static User addTestUser() throws Exception
	{
		// Create new user
		User u = new User();
		u.setEmailAddress("test@example.com");
		u.setFirstName("Test");
		u.setLastName("User");
		u.setGoogleAccountId("testAccountId");
		AddUserResult userResult = (AddUserResult) getService().execute(
			new AddUserAction(u));
		u = userResult.getUser();
		return u;
	}
}

 

Note the call to getService() on line 17. Thanks to Guice, test data generator services can invoke gwt-dispatch ActionHandlers the same way as the tests themselves.

Finally, here’s a test case that calls the above service to create a test user in the Datastore:

 

package com.roa.test;

import java.util.List;

import javax.jdo.Query;

import com.appenginefan.toolkit.unittests.BaseTest;
import com.roa.client.domain.User;
import com.roa.test.service.UserTestDataService;

public class UserTestCase extends BaseTest
{

	@Override
	protected void setUp() throws Exception
	{
		super.setUp();
		UserTestDataService.addTestUser();
	}

	public void testUserAdded() throws Exception
	{
		// Run tests here
		Query q = pm.newQuery(User.class, "emailAddress == e");
		q.declareParameters("java.lang.String e");
		List<User> users = (List<com.roa.client.domain.User>) q.execute("test@example.com");
		assertEquals(1, users.size());
		User user = users.get(0);
		assertNotNull(user.getId());
	}
}

 

I should probably show my BaseTest method, too. I’m using AppEngineFan’s BaseTest as discussed in previous posts and modified the setUp() method for Guice injection as follows:

 

/**
 * Sets up the App Engine environment.
 */
@Override
protected void setUp() throws Exception
{
	if (initializer != null)
	{
		throw new UnsupportedOperationException(
			"setup may only be called once!");
	}
	super.setUp();
	initializer = new TestInitializer(getEnvironmentOrNull());
	initializer.setUp();
	Injector inj = Guice.createInjector(new ServerModule(),
		new DispatchTestModule());
	this.testSvc = inj.getInstance(StandardDispatchService.class);
	this.pm = inj.getInstance(PMF.class).getPersistenceManager();
}

 

Thanks to Guice, we can now get access to the PersistenceManager and call ActionHandlers via a dispatch test service even from static methods in test data generators. This greatly streamlines unit testing.

分享到:
评论

相关推荐

    GWT简介.docx

    NULL 博文链接:https://mydownload.iteye.com/blog/1157105

    gwt-test-utils:gwt-test-utils是一个Java框架,允许以高效,简便的方式测试GWT客户端代码

    怀念Gaël gwt-test-utils是用于应用程序的Java测试框架。 它提供了一种简单的方法来为GWT客户端代码编写快速的Java测试,而无需GWTTestCase或任何servlet容器实例! 这意味着您可以不受限制地使用任何Java工具:...

    gwt-widgets-server1.1.jar

    GWT整合Spring时需要这个包,在官网上没有最新的jar包,这是自己用jar命令生成的,并测试可以使用。

    gwt-maven-plugin:旧版GWT Maven插件

    现在,该插件被认为是legacy GWT maven plugin (又名mojo GWT maven插件),而新插件被认为是new generation GWT maven plugin (又名tbroyer GWT maven插件)。 仍然支持旧版maven插件,但强烈建议将新插件用于新...

    gwt-maven-sample:从https镜像

    * gwt-dispatch. Handlers are spring managed beans. * jsr-330 in client and server side. * Spring. * Spring Security. * H2 database. * Build by maven. * Runnable by jetty (mvn jetty:deploy-war). * Auto...

    Maven2 + gwt 详细配置指南.doc

    变量值:D:\project tools\gwt-windows-1.4.60 变量注:这个变量指定GWT的存放路径 二、把下面的两段分别接到环境变量path与classpath 的最后面 path: ;%MAVEN_HOME%\bin;%GWT_HOME;%JAVA_HOME%\bin;%...

    gwt入门-gwt从这里开始

    C:\程序开发\Java\gwt-windows-1.4.59 doc(文档目录,开发文档和Java API文档) samples(示例代码目录,非常有名的KitchenSink示例代码即在此目录中) about.html about.txt applicationCreator.cmd ...

    gwt-comet-jar包+实例+source.jar包,

    里面东西很多,都是关于GWT-COMET的内容,实现gwt的服务器推技术,包括gwt-comet-examples-1.2.3:google官网上的Test实例;gwt-comet-1.2.3.jar:jar包,gwt-example:聊天实例源代码(.java的),gwt-event-source...

    gwt-dev-plugin for Firefox3.0.xpi

    GWT调试插件,适用于firefox3.x GWT浏览器插件 gwt-dev-plugin for Firefox3.0

    gwt-ex t学习必备资料

    gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-...

    gwt-maven-plugin:开始使用Maven构建GWT项目

    gwt-maven-plugin 该插件旨在通过提供两种特定的打包方式: gwt-lib和gwt-app ,使使用Maven构建GWT项目更加容易。 基本用法 将插件添加到您的POM并启用扩展: &lt; groupId&gt;net.ltgt.gwt.maven&lt;/ groupId&gt; ...

    cypal.studio.for.gwt-1.0

    cypal.studio.for.gwt-1.0

    GWT工具GWT工具GWT工具GWT工具GWT工具GWT工具

    fwefwefwGWT工具GWT工具GWT工具GWT工具GWT工具GWT工具

    gxt-2.1.1-gwt2 最新的

    gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2

    gwt-dev-plugin for IE、FireFox、Chrome

    gwt-dev-plugin是GWT针对浏览器的安装插件,包含IE、FireFox、Chrome的。不需要的匆下,非学习文档

    基于Google.App.Engine(GAE)的Java和GWT应用开发.pdf

    This book is designed to give developers all the information they need to develop their own GAE+GWT applications, with a particular focus on some of the technologies useful for building scalable ...

    Ext GWT 2.0, Beginner's Guide.pdf

    Ext GWT 2.0: Beginner's Guide is a practical book that teaches you how to use the Ext GWT library to its full potential. It provides a thorough, no-nonsense explanation of the Ext GWT library, what ...

    gwt-websocket-exercise:用于GWT培训的简单GWT Websocket练习

    gwt-websocket-exercise 用于GWT培训的简单GWT Websocket练习 依赖于realityforge的gwt-websocket实现: 在Eclipse中设置Realityforge / gwt-websockets项目 在项目中:在类路径上添加GWT SDK,启用GWT支持 删除...

    gwt-dev-plugin-1.26-rc1.xpi for firefox插件

    原地址如下 http://google-web-toolkit.googlecode.com/files/gwt-dev-plugin-1.26-rc1.xpi

Global site tag (gtag.js) - Google Analytics