JUnit、Spring上下文和Webflow
我试图将JUnit配置为与Spring一起工作,但我不能。 我的问题是它找不到webflow文件,所以执行失败。 然而,这听起来很奇怪,因为我实际上并没有测试webflow的东西!我想从控制器开始到数据库。 所以,这就是我正在做的 我通过以下方式创建测试类:JUnit、Spring上下文和Webflow,junit,spring-webflow,Junit,Spring Webflow,我试图将JUnit配置为与Spring一起工作,但我不能。 我的问题是它找不到webflow文件,所以执行失败。 然而,这听起来很奇怪,因为我实际上并没有测试webflow的东西!我想从控制器开始到数据库。 所以,这就是我正在做的 我通过以下方式创建测试类: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:/web-application-config.xml"}) pub
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/web-application-config.xml"})
public class MyControllerToTest {
@Autowired
private MyController ctr;
@Test
....
}
在classpath中,我插入了src/main/resources,web-application-config.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Scans for application @Components to deploy -->
<context:component-scan base-package="com.infoone.mycontrollerpackage"/>
<!-- Imports the configurations of the different infrastructure systems of the application -->
<import resource="webflow-config.xml" />
<import resource="webmvc-config.xml" />
<import resource="data-access-config.xml" />
<import resource="repository-config.xml" />
<import resource="security-config.xml" />
也许,您还需要我的web流配置文件,该文件也位于src/main/resources中,而我的流位于web-INF/flows/*/.xml中,但我也尝试将其移动到src/main/resources,结果相同:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.2.xsd">
<!-- Executes flows: the central entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
<webflow:listener ref="securityFlowExecutionListener" />
<webflow:listener ref="icefacesFlowListener" />
<webflow:listener ref="myFlowListener" />
<webflow:listener ref="myExceptionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/flows">
<webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>
<!-- Configures the Spring Web Flow JSF integration -->
<faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
<!-- Installs a listener that creates and releases the FacesContext for each request. -->
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
<!-- Installs a listener to apply Spring Security authorities -->
<bean id="securityFlowExecutionListener" class="org.springframework.webflow.security.SecurityFlowExecutionListener" />
<bean id="icefacesFlowListener" class="com.icesoft.spring.security.WebflowListener" />
<bean id="myFlowListener" class="com.infoone.myapp.spring.webflow.AutowiringFlowExecutionListener" />
<bean id="myExceptionListener" class="com.infoone.myapp.exception.MyExceptionListener" />
请考虑我也使用JSF和ICEFACTS,我现在不想通过JUnit测试。
谢谢大家! 您的webflow文件可能位于webapp源代码中,而不是Java源代码中,这就是为什么在junit测试中找不到它们的原因
我建议让您的Spring配置文件更加模块化,然后将测试更改为只包含运行测试所需的Spring配置。您好,谢谢,但我不确定我是否了解您所说的模块化。你建议如何组织它们?层次结构?但是,路径看起来不像是webapp的路径,而是来自项目根目录的路径。按体系结构层组织它们。这样,如果您想要执行从控制器到数据库的集成测试,那么您可以包括控制器的Spring配置文件、服务(如果有的话)和数据访问层。您不需要包含视图层的配置文件。但我想它们是这样组织的,但我需要为每种类型的执行操作、单元测试、web层测试等创建一个特殊的应用程序上下文。对吗?
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.2.xsd">
<!-- Executes flows: the central entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
<webflow:listener ref="securityFlowExecutionListener" />
<webflow:listener ref="icefacesFlowListener" />
<webflow:listener ref="myFlowListener" />
<webflow:listener ref="myExceptionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/flows">
<webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>
<!-- Configures the Spring Web Flow JSF integration -->
<faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
<!-- Installs a listener that creates and releases the FacesContext for each request. -->
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
<!-- Installs a listener to apply Spring Security authorities -->
<bean id="securityFlowExecutionListener" class="org.springframework.webflow.security.SecurityFlowExecutionListener" />
<bean id="icefacesFlowListener" class="com.icesoft.spring.security.WebflowListener" />
<bean id="myFlowListener" class="com.infoone.myapp.spring.webflow.AutowiringFlowExecutionListener" />
<bean id="myExceptionListener" class="com.infoone.myapp.exception.MyExceptionListener" />