【问题标题】:JUnit, Spring Context and WebflowJUnit、Spring 上下文和 Webflow
【发布时间】:2012-09-28 14:28:08
【问题描述】:

我正在尝试将 JUnit 配置为与 Spring 一起使用,但我不能。 我的问题是它找不到 webflow 文件,因此执行失败。 但是,这对我来说听起来很奇怪,因为我实际上并没有测试 webflow 的东西!我想从控制器开始到数据库。 所以,这就是我正在做的事情

我这样创建一个测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/web-application-config.xml"})
public class MyControllerToTest {
        @Autowired
    private MyController ctr;

    @Test
    ....
}

在类路径中,我插入了 web-application-config.xml 所在的 src/main/resources。这里是它的内容:

<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" />

当我作为 JUnit 运行 MyControllerToTest 类时,我得到以下信息:

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6419fa] to prepare test instance [com.infoone.myapp.MyControllerToTest@10036f2]
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowExecutor': Cannot resolve reference to bean 'flowRegistry' while setting bean property 'flowDefinitionLocator'; nested exception is         org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/flows/] cannot be resolved to URL because it does not exist[/code]

也许,您还需要我的 web-flow-config 文件,该文件也位于 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 和 Icefaces(我目前不想通过 JUnit 进行测试)。

谢谢!

【问题讨论】:

    标签: junit spring-webflow


    【解决方案1】:

    您的 webflow 文件可能位于 webapp 源而不是 Java 源中,这就是在您的 junit 测试中找不到它们的原因。

    我建议让您的 Spring 配置文件更加模块化,然后更改您的测试以仅包含运行测试所需的那些 Spring 配置。

    【讨论】:

    • 嗨,谢谢,但我不确定我是否明白你所说的模块化。你会建议如何组织它们?分层?
    • 但是,路径看起来不像是 webapp 的路径,而是来自项目根目录的路径。
    • 按架构层组织它们。这样,如果您想执行从控制器到数据库的集成测试,您需要包含控制器、服务(如果有)和数据访问层的 Spring 配置文件。您不需要包含视图层的配置文件。
    • 但我猜它们是这样组织的,但我需要为每种“类型”的执行(操作、单元测试、Web 层测试等)创建一个临时应用程序上下文。对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多