SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。在运行JUnit测试时

时间:2013-01-23 12:04:38

标签: java spring hibernate junit slf4j

我正在使用JUnit构建一些测试用例来测试我的应用程序,但是当我尝试运行它们时,我收到错误:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

问题是,当我运行我的应用程序时,我没有收到该错误,就在我运行测试用例时......

我的应用程序是使用Spring + Hibernate + Java 6构建的

以下是我在pom.xml文件中的依赖项:

<dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- Dependencias Spring -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- Spring security -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.1.0.RELEASE</version>

        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- JSR 303 + Hibernate Validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${org.hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>${org.hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

        <!-- DB Drivers -->
        <dependency>
            <groupId>com.informix</groupId>
            <artifactId>ifxjdbc</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- Utils -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.12</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.15.0-GA</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectj-tools</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>com.commsen.liferay.portal.libraries</groupId>
            <artifactId>axis</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>es.sergas</groupId>
            <artifactId>log4jcomponent</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- Hibernate SQL Logger -->
        <dependency>
            <groupId>es.bahiasoftware</groupId>
            <artifactId>hibernate-sql-logger</artifactId>
            <version>01.00</version>
        </dependency>

        <!-- JUnit -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

有什么想法吗?

3 个答案:

答案 0 :(得分:6)

正如错误消息所说:

  

有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder

...说:

  

无法将org.slf4j.impl.StaticLoggerBinder类加载到内存中时会报告此错误。如果在类路径上找不到合适的SLF4J绑定,则会发生这种情况。在类路径上放置一个(并且只有一个)slf4j-nop.jar,slf4j-simple.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或logback-classic.jar应该可以解决问题。

     

从SLF4J版本1.6开始,在没有绑定的情况下,SLF4J将默认为无操作(NOP)记录器实现。


因此,修复方法是将与其中一个相对应的Maven依赖项添加到POM文件中。唯一的缺点是你需要这种依赖,以具有“测试”的范围。

答案 1 :(得分:4)

您需要为slf4j包含一个后端。 Slf4j是一个外观,它重定向到日志框架,如log4j或logback。在依赖项中添加以下内容:

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>${slf4j.version}</version>
</dependency>

答案 2 :(得分:0)

在我的,可能是罕见的情况下,问题是C:\Program Files (x86)\Java\jreV.V.V_VVV\lib\ext中有许多额外的罐子不应该存在(有人把它放在那里),包括文件slf4j-api-1.7。 12.jar。

只需从...ext\lib删除slf4j-api-V.V.VV.jar即可避免此错误。