4

我正在使用 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>

有任何想法吗?

4

3 回答 3

7

正如错误消息清楚地表明:

有关详细信息,请参阅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 文件中。唯一的问题是您需要此依赖项才能具有“测试”范围。

于 2013-01-23T12:15:47.027 回答
5

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

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>${slf4j.version}</version>
</dependency>
于 2013-01-23T12:18:03.267 回答
0

在我的,可能是罕见的情况下,问题是有一些额外的 jarC:\Program Files (x86)\Java\jreV.V.V_VVV\lib\ext不应该在那里(有人把它们放在那里),包括文件 slf4j-api-1.7.12.jar。

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

于 2017-10-20T06:35:40.177 回答