2017-08-31 66 views
1

我在運行SpringBoot項目中的測試時遇到了一些問題。SpringBoot測試模塊不會在正確的類路徑中搜索log4j2配置

項目結構如下:

Project Structure Image

我就可以開始沒有問題resourceService,但如果我連試運行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) 

於是,我就特定財產

logging.config=resourceservice/log4j2.xml 

改變application.properties到

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模塊不會遭受同樣的問題,而僅僅是因爲我還沒有設置logging.config財產Authorizationservice的application.properties(沒有必要現在)

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

-UPDATE 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這個文件,我會告訴你在這個更新結束)

Exscuse我爲lenght,我使用了大量的庫在這裏。

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' 
} 
+0

請分享Gradle文件。我認爲你沒有正確標記資源文件夾。 – user1615664

+0

更新了帖子。感謝您的回答 – Tommolo

回答

0

我覺得貪婪在回答我自己的問題,但我想我已經解決了。

我做這些事情: - 被測模塊

src 
    main 
    test 
     java 
     resources 
      application.properties 

>創建的資源文件夾中的屬性文件,我只把這個設置

logging.config=log4j2.xml 

和所有我要現在說是 "explicative image here"

只是我必須深入研究彈簧結構(甚至gradle一)關於自動檢測o f屬性文件

0

你缺少你的項目結構src/test/resources文件夾,看起來像當前位置log4j2.xml在項目中根目錄下根本沒有類路徑

依靠Gradle項目約定並將您的配置f爾斯因此,即創造或者SRC /測試/資源,並把文件放在那裏,如果你的日誌記錄配置測試具體

src 
    test 
     java 
     ... 
    resources 
     log4j2.xml 

或者,如果記錄裏面您log4j2.xml將您的應用程序在現場使用配置運行(包含到.jar可交付成果中),將此文件移動到src/main/resources而不是

這將使您的logging.config=log4j2.xml工作。

約搖籃工程結構&資源文件夾,而且閱讀是here