Tobet
Tobet

Reputation: 347

SpringBoot test module doesn't search log4j2 configuration in the right classpath

I'm having some problems running tests in my SpringBoot project.

The project-structure is the following:

Project Structure Image

I can start the resourceService without issues, but if i even try to run the standard test of SpringBoot projects.....

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() {
    }

}

the program respond with this error:

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)

So i tried to change the application.properties specific property from

logging.config=resourceservice/log4j2.xml

to

logging.config=log4j2.xml

After the change i've noticed that resourceserviceApplication will not start because it can't find the 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)

I've tried to resolve in many ways and doing a lot of researches but at the moment i'm still stuck here.

Any idea?

ps: it seems that the Authorizationservice module doesn't suffer the same problem, but simply because i haven't set the logging.config property in Authorizationservice's application.properties (there is no need for now)

Thanks in advance and have a great day.

-UPDATE 1-

Configuration file is about all the resourceService module, so i've done as you said Kostiantyn ( thanks for you response ) but the problem still persist.

Actual situation:

Project structure after your reply

Now resourceServiceApplication won't start , saying :

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

and the contextLoads() method from the test package says:

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

Let me show you configuration file:

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

As requested from user1615664 , below you can see my gradle file (that's the gradle file about the resourceService module; AuthorizationService have one specific gradle file and lastly there is a root gradle file, that i will show to you at the end of this update)

Exscuse me for the lenght, i'm using a large number of libraries here.

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: maybe is worthless , but in the root module ( baseProjectOauth2 ) we can admire this root gradle file

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

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

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

Upvotes: 1

Views: 16113

Answers (3)

Saša
Saša

Reputation: 4808

You can try to put value classpath:log4j2.xml instead of just log4j2.xml into your application.properties (or application.y[a]ml) file.

All the resources, including this file, should be placed inside the src/main/resources (where application.properties file is also placed). Regardless of a few possible exceptions.

If you want a different configuration of logging for tests, than you should put adapted file inside of tests' resources directory (src/test/resources), but if you want these to be the same, then you can leave it as is (inside src/man/resources only)

Upvotes: 0

Kostiantyn
Kostiantyn

Reputation: 1906

You've missing src/test/resources folder in your project structure, and looks like current location of log4j2.xml in the project root is not under classpath at all

Rely on Gradle project convention and place your config files accordingly, i.e. create either src/test/resources and put file in there, if your logging configuration is test specific:

src
   test
      java
         ...
      resources
         log4j2.xml

Or, if logging configuration inside your log4j2.xml will be used by your application during live run (included into .jar deliverable), move this file to src/main/resources instead

that will bring your logging.config=log4j2.xml working.

Further reading about Gradle project structure & resources folders is here

Upvotes: 6

Tobet
Tobet

Reputation: 347

I feel greedy replying to my own question, but i think i've solved.

I've done these things: -> created resource folder under test module

src
    main
    test
        java
        resources
            application.properties

In that properties file i've only put this setup

logging.config=log4j2.xml

And all i have to say now is "explicative image here"

Simply i have to deep study spring structure (and even gradle one) about auto detect of properties files

Upvotes: 2

Related Questions