【问题标题】:No Spring Tests are being run没有运行 Spring 测试
【发布时间】:2012-11-09 10:38:49
【问题描述】:

我有以下 pom.xml surefire 配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <printSummary>true</printSummary>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>

        <includes>
            <include>**/test/resources/META-INF/spring/test-database.properties</include> 
            <include>**/src/test/java/*.java</include>
        </includes>

        <excludes>
            <exclude>**/*_Roo_*</exclude>
            <exclude>**/database-*.properties</exclude>
        </excludes>

    </configuration>
</plugin>

还有下面的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                           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
                           http://www.springframework.org/schema/jee
                           http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<!-- FOR UNIT TESTING ONLY -->

    <!--
        This will automatically locate any and all property files you have
        within your classpath, provided they fall under the META-INF/spring
        directory. The located property files are parsed and their values can
        then be used within application context files in the form of
        ${propertyKey}.
    -->

    <context:property-placeholder location="classpath*:META-INF/spring/test-database.properties"/>

    <bean id="externalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
       <property name="ignoreResourceNotFound"><value>true</value></property>
       <property name="locations">
          <list>
            <value>classpath*:META-INF/conf/configuration.properties</value>
          </list>
       </property>
    </bean>

    <!--
        Turn on AspectJ @Configurable support. As a result, any time you
        instantiate an object, Spring will attempt to perform dependency
        injection on that object. This occurs for instantiation via the "new"
        keyword, as well as via reflection. This is possible because AspectJ
        is used to "weave" Roo-based applications at compile time. In effect
        this feature allows dependency injection of any object at all in your
        system, which is a very useful feature (without @Configurable you'd
        only be able to dependency inject objects acquired from Spring or
        subsequently presented to a specific Spring dependency injection
        method). Roo applications use this useful feature in a number of
        areas, such as @PersistenceContext injection into entities.
    -->
    <context:spring-configured/>
    <!--
        This declaration will cause Spring to locate every @Component,
        @Repository and @Service in your application. In practical terms this
        allows you to write a POJO and then simply annotate the new POJO as an
        @Service and Spring will automatically detect, instantiate and
        dependency inject your service at startup time. Importantly, you can
        then also have your new service injected into any other class that
        requires it simply by declaring a field for your service inside the
        relying class and Spring will inject it. Note that two exclude filters
        are declared. The first ensures that Spring doesn't spend time
        introspecting Roo-specific ITD aspects. The second ensures Roo doesn't
        instantiate your @Controller classes, as these should be instantiated
        by a web tier application context. Refer to web.xml for more details
        about the web tier application context setup services.

        Furthermore, this turns on @Autowired, @PostConstruct etc support. These 
        annotations allow you to use common Spring and Java Enterprise Edition 
        annotations in your classes without needing to do any special configuration. 
        The most commonly used annotation is @Autowired, which instructs Spring to
        dependency inject an object into your class.
    -->

    <context:component-scan base-package="com.mms.pone.portal">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <!--<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
        <property name="validationQuery" value="SELECT 1"/> 
        <property name="initialSize" value="5" />
        <property name="maxActive" value="25" />
    </bean> -->

     <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>

        <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" />
    </bean>    
</beans>

一切似乎都正常,但是当我运行 mvn clean install 或 mvn test 时,文件编译,没有显示错误并显示摘要 Tests Run: 0。

非常欢迎任何帮助。

【问题讨论】:

  • includes/excludes 标签应该描述具有测试类的文件的模式。 .properties 文件应该在那里。看这里:maven.apache.org/plugins/maven-surefire-plugin/examples/…
  • @TheCat 这就是我在评论中链接的内容 ;-)
  • 问题在于,使用包含标签实际上是在说要运行 src/test/java/ 文件夹中的所有测试类。不在任何子文件夹中!因此,如果您在 src/test/java/my/package/ 中有测试类,它们将被有效地排除在外。
  • 我想说跳过surefire插件中的那些includes/excludes部分。然后确保您的测试类以Test 结束或开始。之后您可以告诉我们您的真正问题是什么。
  • 如果你在你的项目中使用 spring-roo 那么这个 exclude &lt;exclude&gt;**/*_Roo_*&lt;/exclude&gt; 应该在那里。

标签: java spring maven spring-mvc surefire


【解决方案1】:

我终于找到了这个链接,发现问题出在我的 persistence.xml 和 applicationContext.xml 中的双重属性声明。清理完这些文件并查看我的文件后:

applicationContext.xml:

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="testWhileIdle" value="false"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="maxActive" value="1" />

        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>

persistence.xml

  <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create"/>
    </properties>

一切运行良好。

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2014-10-25
    • 2018-07-08
    • 2018-06-01
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多