如何在JUnit测试中关闭弹簧记录?

时间:2013-06-21 16:19:21

标签: spring hibernate logging junit log4j

我正在使用Maven 3.0.3,JUnit 4.8.1,Spring 3.1.1.RELEASE和Hibernate 4.1.0.Final。如何在运行JUnit测试时关闭Spring日志消息?我收到此类消息如

Jun 21, 2013 10:54:34 AM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/admin/standards/import],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void org.mainco.subco.standards.mvc.StandardsImportController.standardsImportForm()

我在src / test / resources目录中有这个log4j.properties文件...

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=ERROR, stdout

# Hibernate logging options (INFO only shows startup messages)
log4j.logger.org.hibernate=ERROR

# Log JDBC bind parameter runtime arguments
log4j.logger.org.hibernate.type=ERROR

log4j.category.org.springframework=ERROR
log4j.category.net.sf.hibernate=ERROR

日志消息仍在涌出。这是我在JUnit测试中使用的应用程序上下文文件。知道如何消除噪音吗?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                    http://www.springframework.org/schema/jdbc 
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

    <import resource="classpath:/META-INF/spring/applicationContext-user.xml"/>
    <import resource="classpath:/META-INF/spring/applicationContext-session.xml"/> 
    <import resource="classpath:/META-INF/spring/applicationContext-session-cache.xml"/>
    <import resource="classpath:/META-INF/spring/applicationContext-sbadmin-security.xml"/>
    <import resource="classpath:/META-INF/spring/applicationContext-sbadmin-mvc.xml"/> 

    <!-- Define test properties -->
    <util:properties id="testProps" location="classpath:test.properties" />
    <context:property-placeholder location="classpath:session.properties,classpath:ebook.properties,classpath:mysql_datasource.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${test.mysql.dataSource.driverClassName}" />
        <property name="url" value="${test.mysql.dataSource.url}" />
        <property name="username" value="${test.mysql.db.user}" />
        <property name="password" value="${test.mysql.db.password}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="org.mainco.subco" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaPropertyMap" ref="jpaPropertyMap" />
    </bean>

    <util:map id="jpaPropertyMap">
        <entry key="hibernate.show_sql" value="false" />
        <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <entry key="hibernate.hbm2ddl.auto" value="validate" /> 
        <entry key="hibernate.cache.region.factory_class"   value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
        <entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
        <entry key="hibernate.cache.use_second_level_cache" value="true" />
        <entry key="hibernate.cache.use_query_cache" value="false" />
        <entry key="hibernate.generate_statistics" value="true" />
    </util:map>

    <bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

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

    <tx:annotation-driven />

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:truncate_tables.sql"/> 
        <jdbc:script location="classpath:db-test-data.sql"/>    
    </jdbc:initialize-database>

</beans>

3 个答案:

答案 0 :(得分:0)

尝试将log4j.category.org.springframework=ERROR更改为log4j.logger.org.springframework=ERROR

答案 1 :(得分:0)

有趣的是,我在我的pom.xml文件中有这两个依赖项......

            <dependency>
                    <groupId>org.apache.openejb</groupId>
                    <artifactId>commons-dbcp-all</artifactId>
                    <version>1.3</version>
                    <scope>test</scope>
            </dependency>
            <dependency>
                    <groupId>org.apache.openejb</groupId>
                    <artifactId>openejb-core</artifactId>
                    <version>4.0.0</version>
                    <scope>test</scope>
            </dependency>

删除这些依赖项(我不需要),解决了我的日志记录问题。

答案 2 :(得分:0)

事实上,我发现Logback很简单:

只需添加/更改这些:

<logger name="org.hibernate" level="OFF" additivity="false">
    <appender-ref ref="CONSOLE"/>
</logger>

<logger name="org.springframework" level="OFF" additivity="false">
    <appender-ref ref="CONSOLE"/>
</logger>

虽然我没有经过测试,但我认为在log4j中将关卡设置为OFF也应该有效。