gpt4 book ai didi

java - Struts2 + Spring3 + JUnit4 集成

转载 作者:行者123 更新时间:2023-12-01 04:15:01 37 4
gpt4 key购买 nike

我尝试为 Struts2 和 Spring3 集成编写单元测试。我使用struts2-junit插件进行jUnit4测试。但问题是操作代理返回 null。

如何解决?

下面的代码是我的测试类和其他配置。 测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext.xml" })
public class StrutsTest extends StrutsSpringJUnit4TestCase<MyAction> {
@Test
public void myTest() throws Exception {
ActionProxy proxy = getActionProxy("/myaction");
MyAction myAction = (MyAction) proxy.getAction();
request.setParameter("personBean.firstName", "Joe");
request.setParameter("personBean.lastName", "Doe");
String result = myAction.execute();
assertEquals("Error", ActionSupport.SUCCESS, result);
}
}

MyAction.java:

@Component
@Scope("prototype")
public class MyAction implements Action {
private Person personBean;

public String execute() throws Exception {
System.out.println("MyAction is called!");
System.out.println("personBean.firstName"+personBean.getFirstName());
System.out.println("personBean.firstName"+personBean.getFirstName());
return ActionSupport.ERROR;
}

}

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Constants -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.custom.i18n.resources" value="applicationResources, actionErrorMapping, smsMessages" />
<constant name="struts.codebehind.pathPrefix" value="/WEB-INF/" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.multipart.maxSize" value="2097152" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
<action name="myaction" class="com.struts.actions.MyAction">
<result>/index.jsp</result>
</action>
</package>
</struts>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com" />
</beans>

最佳答案

  1. 确保您已包含 struts2-spring-plugin:

    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>STRUTS_VERSION</version>
    </dependency>
  2. 在 web.xml 中尝试使用

    <param-value>classpath*:applicationContext.xml</param-value>
  3. 在 struts.xml 中尝试使用

    <constant name="struts.objectFactory" 
    value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

我第一次尝试...希望有帮助。

关于java - Struts2 + Spring3 + JUnit4 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19518997/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com