【问题标题】:SpringBoot test module doesn't search log4j2 configuration in the right classpathSpringBoot 测试模块不在正确的类路径中搜索 log4j2 配置
【发布时间】:2017-08-31 14:03:47
【问题描述】:

我在 SpringBoot 项目中运行测试时遇到了一些问题。

项目结构如下:

Project Structure Image

我可以毫无问题地启动资源服务,但如果我什至尝试运行 SpringBoot 项目的标准测试.....

package com.pcsystem;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ResourceserviceApplicationTests {

    @Test
    public void contextLoads() {
    }

}

程序以这个错误响应:

Logging system failed to initialize using configuration from 'resourceservice/log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\resourceservice\log4j2.xml (Unable to find the specified classpath)

所以我尝试从

更改 application.properties 特定属性
logging.config=resourceservice/log4j2.xml

logging.config=log4j2.xml

更改后我注意到 resourceserviceApplication 将无法启动,因为它找不到 log4j2.xml:

Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml (Unable to find the specified classpath)

我尝试通过多种方式解决问题并进行了大量研究,但目前我仍然被困在这里。

有什么想法吗?

ps: Authorizationservice 模块似乎没有遇到同样的问题,只是因为我没有在 Authorizationservice 的 application.properties 中设置 logging.config 属性(暂时不需要)

提前致谢,祝您有美好的一天。

-更新 1-

配置文件是关于所有资源服务模块的,所以我已经按照你所说的 Kostiantyn 完成了(感谢你的回复),但问题仍然存在。

实际情况:

Project structure after your reply

现在resourceServiceApplication 不会启动,说:

Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml 

测试包中的 contextLoads() 方法说:

java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\log4j2.xml 

让我给你看配置文件:

server.port=8888

logging.config=log4j2.xml

spring.data.mongodb.host=localhost
spring.data.mongodb.database=jogging
#spring.data.mongodb.username=admin
#spring.data.mongodb.password=pass
spring.data.mongodb.port=27017

根据 user1615664 的要求,您可以在下面看到我的 gradle 文件 (这是关于 resourceService 模块的 gradle 文件; AuthorizationService 有一个特定的 gradle 文件,最后有 一个根 gradle 文件,我将在本次更新结束时向您展示)

对不起,我在这里使用了大量的库。

buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    compile.exclude group:'ch.qos.logback'
}

dependencies {


    compile('org.springframework.boot:spring-boot-starter-data-mongodb')
    compile('org.springframework.boot:spring-boot-starter-security')

    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude module: "logback-classic"
    }
    compile('org.springframework.boot:spring-boot-starter-log4j2')

    compile group: 'org.apache.tika', name: 'tika', version: '1.16', ext: 'pom'
    compile group: 'org.apache.tika', name: 'tika-parsers', version: '1.16'
    compileOnly('org.projectlombok:lombok')

    compile("org.springframework.boot:spring-boot-starter-actuator")


    //compile project(':authorizationservice')
    // https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas
    compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version: '0.23.0.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
    compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.1.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.8.RELEASE'

    // https://mvnrepository.com/artifact/org.apache.axis/axis
    compile group: 'org.apache.axis', name: 'axis', version: '1.4'
    // https://mvnrepository.com/artifact/axis/axis-jaxrpc
    compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'

    // https://mvnrepository.com/artifact/commons-discovery/commons-discovery
    compile group: 'commons-discovery', name: 'commons-discovery', version: '0.5'
    // https://mvnrepository.com/artifact/org.slf4j/slf4j-api

    // https://mvnrepository.com/artifact/javax.xml/jaxrpc-api
    compile group: 'javax.xml', name: 'jaxrpc-api', version: '1.1.1'

    // https://mvnrepository.com/artifact/org.apache.xmlrpc/xmlrpc
    compile group: 'org.apache.xmlrpc', name: 'xmlrpc', version: '3.1.3', ext: 'pom'
    // https://mvnrepository.com/artifact/javax.activation/activation
    compile group: 'javax.activation', name: 'activation', version: '1.1.1'
    // https://mvnrepository.com/artifact/javax.mail/mail
    compile group: 'javax.mail', name: 'mail', version: '1.4.7'
    // https://mvnrepository.com/artifact/wsdl4j/wsdl4j
    compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.3'



    testCompile('org.springframework.boot:spring-boot-starter-test')
}

PS: 可能毫无价值,但在根模块 (baseProjectOauth2) 我们可以欣赏这个根 gradle 文件

group 'com.pcsystem'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

【问题讨论】:

  • 请分享 Gradle 文件。我认为您没有正确标记资源文件夹。
  • 更新了帖子。感谢您的回答

标签: java testing logging spring-boot log4j2


【解决方案1】:

您的项目结构中缺少src/test/resources 文件夹,并且看起来项目根目录中log4j2.xml 的当前位置根本不在类路径下

依赖 Gradle 项目约定并相应地放置您的配置文件,即创建 src/test/resources 并将文件放入其中,如果您的日志记录配置是特定于测试的

src
   test
      java
         ...
      resources
         log4j2.xml

或者,如果您的应用程序在实时运行期间将使用您的 log4j2.xml 中的日志记录配置(包含在可交付的 .jar 中),请将此文件移至 src/main/resources

这将使您的logging.config=log4j2.xml 正常工作。

关于 Gradle 项目结构和资源文件夹的进一步阅读是 here

【讨论】:

    【解决方案2】:

    我很想回答我自己的问题,但我想我已经解决了。

    我做过这些事情: -> 在测试模块下创建资源文件夹

    src
        main
        test
            java
            resources
                application.properties
    

    在那个属性文件中我只放了这个设置

    logging.config=log4j2.xml
    

    我现在要说的是 "explicative image here"

    我必须深入研究关于属性文件自动检测的弹簧结构(甚至是 gradle 一)

    【讨论】:

      【解决方案3】:

      您可以尝试将值 classpath:log4j2.xml 而不仅仅是 log4j2.xml 放入您的 application.properties(或 application.y[a]ml)文件中。

      所有资源,包括这个文件,都应该放在src/main/resources 中(application.properties 文件也放在这个位置)。不管有几个可能的例外。

      如果你想要一个不同的测试日志配置,你应该把修改后的文件放在测试的资源目录中(src/test/resources),但是如果你希望它们相同,那么你可以保持原样(仅限src/man/resources 内)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2011-04-01
        • 1970-01-01
        • 2016-12-23
        • 2013-07-14
        • 1970-01-01
        • 2020-08-06
        相关资源
        最近更新 更多