使用Gradle Build在Spring Boot Project中配置单元测试的日志记录

时间:2014-08-03 13:13:28

标签: java gradle logback spring-boot

我已经使用spring boot在Gradle中配置了一个Web应用程序,我正在通过STS直接运行测试,但是单元测试没有获取自定义日志记录,它与Gradle构建期间运行测试的情况相同。如何自定义日志记录,以便单元测试使用我在src / test / resources中添加的logback.xml,同时从STS单独运行测试或作为Gradle Build的一部分运行测试?

我的Gradle文件如下

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/libs-release" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.4.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
apply plugin: 'spring-boot'
apply plugin: 'war'

eclipse{
        classpath {
            downloadSources=true
    }
}

war {
    baseName = 'wiggle'
    version =  '0.0.1-BUILD-SNAPSHOT'
}

repositories {
    mavenLocal()
    mavenCentral()
    maven { url "http://repo.spring.io/libs-release" }
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-social-facebook")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("com.jolbox:bonecp:0.8.0.RELEASE")
    // Test Dependencies
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.12'
}

我有一个自定义的logback.xml(将所有内容都放在调试模式下)

Anadis-MacBook-Pro:wiggle anadi$ cat src/test/resources/logback.xml 
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework">
        <level value="DEBUG" />
    </logger>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="CONSOLE" />
    </root>

然而,当我通过Gradle或直接从STS运行测试时,日志记录对Spring不起作用,我得到的输出是

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/anadi/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Applications/springsource/sts-3.5.1.RELEASE/plugins/org.springsource.ide.eclipse.gradle.toolingapi_3.5.1.201404300713-RELEASE/lib/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
2014-08-03 18:37:29,921 DEBUG | main | o.s.test.context.junit4.SpringJUnit4ClassRunner         | SpringJUnit4ClassRunner constructor called with [class wiggle.test.system.persistence.MemberPersistenceSystemTest]. 
2014-08-03 18:37:30,215 DEBUG | main | org.springframework.test.context.ContextLoaderUtils     | Found explicit ContextLoader class [org.springframework.test.context.support.AnnotationConfigContextLoader] for context configuration attributes [ContextConfigurationAttributes@15c955b7 declaringClass = 'wiggle.test.system.persistence.MemberPersistenceSystemTest', locations = '{}', classes = '{class wiggle.test.config.wiggleTestPersistenceContext}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.support.AnnotationConfigContextLoader'] 
2014-08-03 18:37:30,356 DEBUG | main | org.springframework.test.context.ContextLoaderUtils     | Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,500 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved @ProfileValueSourceConfiguration [null] for test class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,501 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,520 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved @ProfileValueSourceConfiguration [null] for test class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,520 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,539 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved @ProfileValueSourceConfiguration [null] for test class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,539 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,541 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved @ProfileValueSourceConfiguration [null] for test class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,541 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,543 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved @ProfileValueSourceConfiguration [null] for test class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,543 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,572 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved @ProfileValueSourceConfiguration [null] for test class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,573 DEBUG | main | org.springframework.test.annotation.ProfileValueUtils   | Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [wiggle.test.system.persistence.MemberPersistenceSystemTest] 
2014-08-03 18:37:30,690 DEBUG | main | o.s.t.c.support.DirtiesContextTestExecutionListener     | After test method: context [DefaultTestContext@6e200e2d testClass = MemberPersistenceSystemTest, testInstance = wiggle.test.system.persistence.MemberPersistenceSystemTest@7a82e4d6, testMethod = test@MemberPersistenceSystemTest, testException = java.lang.NullPointerException, mergedContextConfiguration = [MergedContextConfiguration@277ddc70 testClass = MemberPersistenceSystemTest, locations = '{}', classes = '{class wiggle.test.config.wiggleTestPersistenceContext}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.AnnotationConfigContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [true]. 
2014-08-03 18:37:30,816 DEBUG | main | o.s.t.c.support.DirtiesContextTestExecutionListener     | After test class: context [DefaultTestContext@6e200e2d testClass = MemberPersistenceSystemTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@277ddc70 testClass = MemberPersistenceSystemTest, locations = '{}', classes = '{class wiggle.test.config.wiggleTestPersistenceContext}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.AnnotationConfigContextLoader', parent = [null]]], dirtiesContext [false].

0 个答案:

没有答案