j2ee绿洲

找到属于自己的一片天空
数据加载中……
WebWork中的Junit

你有很多方法可以对WebWork的action进行单元测试.

最简单的就是实例化你的action,调用setter并调用execute(). 这允许你绕过所有复杂的容器设置.

从 Petsoar 里拿的:

package org.petsoar.actions.inventory;
import com.mockobjects.constraint.IsEqual;
import com.mockobjects.dynamic.C;
import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork.Action;
import junit.framework.TestCase;
import org.petsoar.pets.Pet;
import org.petsoar.pets.PetStore;
public class TestViewPet extends TestCase {
private Mock mockPetStore;
private ViewPet action;
protected void setUp() throws Exception {
mockPetStore = new Mock(PetStore.class);
PetStore petStore = (PetStore) mockPetStore.proxy();
action = new ViewPet();
action.setPetStore(petStore);
}
public void testViewPet() throws Exception {
Pet existingPet = new Pet();
existingPet.setName("harry");
existingPet.setId(1);
Pet expectedPet = new Pet();
expectedPet.setName("harry");
expectedPet.setId(1);
mockPetStore.expectAndReturn("getPet", C.args(new IsEqual(new Long(1))), existingPet);
action.setId(1);
String result = action.execute();
assertEquals(Action.SUCCESS, result);
assertEquals(expectedPet, existingPet);
mockPetStore.verify();
}
public void testViewPetNoId() throws Exception {
mockPetStore.expectAndReturn("getPet", C.ANY_ARGS, null);
String result = action.execute();
assertEquals(Action.ERROR, result);
assertEquals(1, action.getActionErrors().size());
assertEquals("Invalid pet selected.", action.getActionErrors().iterator().next());
assertNull(action.getPet());
mockPetStore.verify();
}
public void testViewPetInvalidId() throws Exception {
action.setId(-1);
testViewPetNoId();
}
}

测试拦截器 或/和 result类型

检查XWork/WebWork的测试套件.它们是非常全面并且提供了一个好的开端.例如,这是 ParametersInterceptor 测试的例子:

public void testDoesNotAllowMethodInvocations() {
Map params = new HashMap();
params.put("@java.lang.System@exit(1).dummy", "dumb value");
HashMap extraContext = new HashMap();
extraContext.put(ActionContext.PARAMETERS, params);
try {
ActionProxy proxy = ActionProxyFactory.getFactory().
createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, extraContext);
assertEquals(Action.SUCCESS, proxy.execute());
ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
TestBean model = (TestBean) action.getModel();
String property = System.getProperty("webwork.security.test");
assertNull(property);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}

注意:这不是你唯一的途径.

posted on 2007-06-21 14:16 心情经纬 阅读(302) 评论(0)  编辑  收藏 所属分类: webwork


只有注册用户登录后才能发表评论。


网站导航: