论坛首页 Java企业应用论坛

webwork2_spring做工厂_testcase如何写

浏览 4116 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-03-01  
webwork 2.2.1 ,采用了

webwork.objectFactory=spring


package tiyi.position.actions;

import tiyi.position.mgr.PersonMgr;
import tiyi.position.po.Person;

import com.opensymphony.xwork.ActionSupport;

public class AddPerson extends ActionSupport {

	private PersonMgr personMgr;

	private Person person;

	public PersonMgr getPersonMgr(); {
		return personMgr;
	}

	public Person getPerson(); {
		return person;
	}

	public void setPerson(Person person); {
		this.person = person;
	}

	public void setPersonMgr(PersonMgr personMgr); {
		this.personMgr = personMgr;
	}

	public String execute(); throws Exception {

		personMgr.savePerson(person);;
		return "success";
	}

}


xwork.xml包含
<action name="addPerson" class="tiyi.position.actions.AddPerson"/>


webwork.properties
webwork.objectFactory=spring



applicationContext.xml包含

	<bean id="personMgrTarget" class="tiyi.position.mgr.impl.PersonMgrImpl" autowire="byName"></bean>
	<bean id="personMgr" parent="TransactionProxyFactoryBean">
		<property name="target">
			<ref local="personMgrTarget" />
		</property>
	</bean>

用如下代码

public void testSavePerson(); throws Exception {
		Map parameters = new HashMap();;

		// parameters.put("testBean.attr1", "test1");;
		Map extraContext = new HashMap();;
		extraContext.put(ActionContext.PARAMETERS, parameters);;
		ActionProxy actionProxy = ActionProxyFactory.getFactory();
				.createActionProxy("/", "addPerson", extraContext);;
		String s = actionProxy.execute();;
		assertEquals("save1", s);;

	}



提示:
java.lang.NullPointerException
at tiyi.position.actions.AddPerson.execute(AddPerson.java:32)

32行是:personMgr.savePerson(person);
看来是spring没有注入。(跟踪 personMgr 为null)

请问测试代码该如何写才能注入
   发表时间:2006-03-01  
how about this
AddPerson action = new AddPerson();;
PersonMgr personMgr = /* some mock personMgr */
action.setPersonMgr(personMgr);;
// test your action
0 请登录后投票
   发表时间:2006-03-02  
gigix 写道
how about this
AddPerson action = new AddPerson();;
PersonMgr personMgr = /* some mock personMgr */
action.setPersonMgr(personMgr);;
// test your action

yes, just like this!
0 请登录后投票
   发表时间:2006-03-02  
透明阿,可是我也想检测一下俺们的配置(xwork.xml)有没有问题。所以才用ActionProxyFactory
0 请登录后投票
   发表时间:2006-03-02  
tiyi 写道
透明阿,可是我也想检测一下俺们的配置(xwork.xml)有没有问题。所以才用ActionProxyFactory

测配置就单独些个case测配置,不要跟逻辑搅在一起。
0 请登录后投票
   发表时间:2006-03-03  
可是。应该如何检查service 的getter和setter呢。
总是想把配置阿,注入阿什么的都用测试搞定嘛。
0 请登录后投票
   发表时间:2006-03-04  
tiyi 写道
可是。应该如何检查service 的getter和setter呢。
总是想把配置阿,注入阿什么的都用测试搞定嘛。

integration test with selenium
0 请登录后投票
   发表时间:2006-03-06  
tiyi 写道
透明阿,可是我也想检测一下俺们的配置(xwork.xml)有没有问题。所以才用ActionProxyFactory


Xwork这层抽象本就是为了让你脱离servlet环境复用和测试。Webwork与Spring的集成是通过servlet里的applicationContext来获取Spring的appContext的。所以你如果需要两者运行需要在你的测试里面初始化Spring并将appContext注入到SpringObjectFactory里面。
使用Xwork里面SpringObjectFactory的public void setApplicationContext(ApplicationContext appContext)。

不过感觉这样并不好,如gigix所说配置应该在integration test来做。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics