---
title: "JBoss Seam项目测试 "
author: "happylo"
date: "2009-02-13T12:57:48Z"
tags: []
slug: "jboss-seam"
draft: false
lastmod: "2009-02-17T13:55:39Z"
id: 328099
url: "https://happydev.iteye.com/blog/328099"
---

\[原创于：[http://happydev.iteye.com](http://happydev.iteye.com/)]

 

按Seam参考手册，JBoss Seam项目的测试可以使用org.jboss.seam.mock.SeamTest来完成。

但在实际的使用过程中还真的很是折腾人，下面将这个过程描述如下，让各位看官少走的弯路。

 

一、环境

eclipse-jee-ganymede-SR1-win32

jboss-seam-2.1.1.GA

JBossTools-3.0.0.Beta1-ALL-win32

jboss-4.2.3.GA-jdk6

 

二、示例工程准备

测试工程就是直接在开发环境中使用Seam Web Project向导创建一个标准示例工程。

然后使用Seam Entity向导快速创建一个标准的对User实体的CRUD操作实体类、列表页、编辑页。

在使用Seam Web Project向导创建一个标准示例工程时，会自动创建一个相关的测试工程。

示例工程的实体如下：

 

 

```
@Entity
public class User implements Serializable
{
    // seam-gen attributes (you should probably edit these)
    private Long id;
    private Integer version;
    private String name;

    // add additional entity attributes

    // seam-gen attribute getters/setters with annotations (you probably should edit)

    @Id @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    private void setVersion(Integer version) {
        this.version = version;
    }

    @Length(max = 20)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

```

 

 其它的由向导生成的UserHome.java、UserList.java、user.xhtml、userList.xhtml就不在这一一列出了。

 

三、测试代码

基于上面的示例工程，写的测试代码如下：

 

```
import java.util.List;

import org.jboss.seam.mock.SeamTest;
import org.testng.annotations.Test;

import cn.test.entity.User;

public class UserTest extends SeamTest {
	@Test
	public void testAddUser() throws Exception {
		new NonFacesRequest("/userList.xhtml") {
			protected void renderResponse() throws Exception {
				Object value = getValue("#{userList.resultList}");
				assert value != null && value instanceof List;
				List<User> users = (List<User>) value;
				assert users.size() == 0;
			}
		}.run();
		new FacesRequest("/user.xhtml") {
			protected void updateModelValues() throws Exception {
				setValue("#{userHome.instance.name}", "Test");
			}

			protected void invokeApplication() throws Exception {
				invokeMethod("#{userHome.persist}");
			}

			protected void renderResponse() throws Exception {
				Object value = getValue("#{userList.resultList}");
				assert value != null && value instanceof List;
				List<User> users = (List<User>) value;
				assert users.size() == 1;
			}
		}.run();
	}
}

```

 

 

四、运行测试

在运行测试过程中遇到了以下三个麻烦：

1、一开始运行测试，便直接报以下错误

 

 写道

\[Parser] Running:  
C:\\test\\workspace\_demo\\FF-test\\temp-testng-customsuite.xml

ERROR  
\[org.jboss.kernel.plugins.dependency.AbstractKernelController] Error installing to Described: name=BeanDeployer st  
ate=PreInstall

 

 根据查网上资料，是因为JDK版本的原因，需在JVM上加上以下参数：

 

 写道

-Dsun.lang.ClassLoader.allowArraySyntax=true

 

 

2、再运行测试，还是报错

 写道

Connection timed out

 再找原因，是因为在启动嵌入式JBoss时要到网上查找一个DTD文件，但我的本机上网需要代理，于是在JVM下继续添加以下参数：

 

 写道

-Dhttp.proxyHost=x.x.x.x -Dhttp.proxyPort=yyyy

 

 3、再运行测试，嵌入式JBoss已正常启动，但加载Seam工程时出错了

 

 写道

Two components with the same name and precedence

 

 再找原因，得知是jboss-seam-2.1.1.GA版本的Bug，解决办法是将Seam工程中的seam.properties删除即可，一试，果然，郁闷中...

 

五、测试成功

ok