java - SpringBoot 测试模块不在正确的类路径中搜索 log4j2 配置

标签 java testing logging spring-boot log4j2

我在我的 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-

配置文件是关于所有resourceService模块的,所以我已经按照你说的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: 可能毫无值(value),但在根模块 (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'
}

最佳答案

您的项目结构中缺少 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

关于java - SpringBoot 测试模块不在正确的类路径中搜索 log4j2 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45983414/

相关文章:

javascript - Cypress :根据另一个子元素获取父元素的子元素

node.js - 在 tsconfig.json 排除字段中添加测试文件是一个好的做法吗?

python - 队列消费者打印与日志记录

Java分布式架构和版本控制

c# - 如何最好地模拟 IO 绑定(bind)工作?

java - 使用 JMX 注册和更新应用程序属性

powershell - 在Powershell中查找外部IP地址和日志地址号?

python - 使用 Python 日志记录管理记录器

java - 计算日期之间的天数差异

java - 使用 Spring security oauth,使用自定义 OAuth 提供程序,我得到 [authorization_request_not_found],我应该自己处理回调方法吗?