Log4j在测试类中不起作用

kqqjbcuj  于 2023-01-26  发布在  其他
关注(0)|答案(3)|浏览(106)

我有一个 selenium 测试的源文件夹:* * src/selenium/java**和我在下面有单元测试类,实现如下:

package com.myapp.selenium;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HomeTest {

    static WebDriver driver;
    protected static final Logger logger = LoggerFactory.getLogger(HomeTest.class);

    @BeforeClass
    public static void before() {
        if(logger.isInfoEnabled())
            logger.info("Running Selenium Test for class"+HomeTest.class.getSimpleName());
        driver = new FirefoxDriver();
    }

    @AfterClass
    public static void after() {
        driver.close();
    }

    @Test
    public void addingOneUser() throws InterruptedException {
        if(logger.isInfoEnabled())
            logger.info("addingOneUser");
        driver.get("http://localhost:8080/MyApp/index.xhtml");  
    }

}
    • log4j.属性**:
log4j.rootLogger=INFO, R, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F%L) - %m%n  

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=application.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.com.myapp=DEBUG
log4j.logger.org.hibernate=debug

log4j罐

'org.slf4j:slf4j-api:1.7.2'
'org.slf4j:jcl-over-slf4j:1.7.2'
'org.slf4j:slf4j-log4j12:1.7.2'
    • 更新日期:**

我的Gradle.构建版本

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'
apply plugin: 'findbugs'
//apply from:'http://github.com/breskeby/gradleplugins/raw/master/emmaPlugin/emma.gradle'
apply from: 'emma.gradle'
buildDir = 'build'

sourceCompatibility = 1.7
version = ''

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'org.gradle.api.plugins:gradle-cargo-plugin:0.6'
    }
}

repositories {
    mavenCentral()
    mavenRepo url: 'http://repository.primefaces.org'
    mavenRepo url: 'http://repository.jboss.org/nexus/content/groups/public'
    mavenRepo url: 'http://repository.jboss.org/maven2'
    mavenRepo url: 'http://maven.springframework.org/release'
    mavenRepo url: 'http://repo1.maven.org/maven2'
    mavenRepo url: 'http://git.solutionstream.com/nexus/content/repositories/thirdparty'
}

configurations {
   compileOnly
   weldEmbeddedTestRuntime { extendsFrom testRuntime }
   jbossasRemoteTestRuntime { extendsFrom testRuntime, compileOnly }
}

sourceSets {

   main {
      compileClasspath = configurations.compile + configurations.compileOnly
   }

   test {
      compileClasspath = compileClasspath + configurations.compileOnly
   }

   selenium {
      compileClasspath = compileClasspath + configurations.compileOnly
   }
}

dependencies {
    //JSF
    compile group: 'com.sun.faces', name: 'jsf-api', version: '2.1.22'
    compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.1.22'
    compile 'org.ocpsoft.rewrite:rewrite-servlet:2.0.3.Final'
    compile 'org.ocpsoft.rewrite:rewrite-config-prettyfaces:2.0.3.Final'

    //Servlet
    compile group: 'javax.servlet', name: 'jstl', version: '1.2'
    providedCompile group: 'org.jboss.spec', name: 'jboss-javaee-6.0', version: '1.0.0.Final'
    compile 'taglibs:standard:1.1.2'
    compile group: 'org.springframework', name: 'spring-web', version: '3.2.2.RELEASE'

    //Omnifaces
    compile 'org.omnifaces:omnifaces:1.5'

    //Prime Faces
    compile group: 'org.primefaces', name: 'primefaces', version: '4.0-SNAPSHOT'
    compile 'org.primefaces.themes:bootstrap:1.0.10'

    // DB
    compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.3.1.RELEASE'
    compile group: 'org.springframework', name: 'spring-aspects', version: '3.2.2.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.9'

    compile group: 'javax.inject', name: 'javax.inject', version: '1'
    compile group: 'javax.enterprise', name: 'cdi-api', version: '1.0-SP4'
    compile 'cglib:cglib-nodep:2.2.2'

    //Hibernate / JPA   
    compile 'org.hibernate:hibernate-core:4.1.0.Final'
    compile 'org.hibernate:hibernate-entitymanager:4.1.0.Final'
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'
    //JSR-303
    compile 'org.hibernate:hibernate-validator:4.3.1.Final'

    // Spring Security
    compile 'org.springframework.security:spring-security-core:3.1.4.RELEASE'
    compile 'org.springframework.security:spring-security-web:3.1.4.RELEASE'
    compile 'org.springframework.security:spring-security-config:3.1.4.RELEASE'

    //Utility
    compile 'com.google.guava:guava:14.0.1'
    compile 'commons-lang:commons-lang:2.6'
    compile 'org.apache.commons:commons-email:1.3.1'
    compile 'com.typesafe:config:1.0.0'
    compile 'joda-time:joda-time:2.2'
    compile 'org.apache.geronimo.javamail:geronimo-javamail_1.4_mail:1.8.3'
    compile 'org.slf4j:slf4j-api:1.7.2'
    compile 'org.slf4j:jcl-over-slf4j:1.7.2'
    compile 'org.slf4j:slf4j-log4j12:1.7.2'

    //Mustache Templates
    compile 'com.github.jknack:handlebars:1.0.0'

    //Projects
    //compile project(":ExtraValidators")

    ////TESTING DEPENDENCIES
    testCompile 'com.googlecode.jmockit:jmockit:1.2'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'com.h2database:h2:1.3.172'

    //Spring Testing
    testCompile 'org.springframework:spring-test:3.2.3.RELEASE'

    /* Selenium */
    seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.33.0'
    seleniumCompile 'junit:junit:4.11'
    seleniumCompile 'org.slf4j:slf4j-api:1.7.2'
    seleniumCompile 'org.slf4j:slf4j-log4j12:1.7.2'
    seleniumCompile 'org.slf4j:jcl-over-slf4j:1.7.2'

    /* Remote Jboss */
    testCompile group: 'org.jboss.arquillian', name: 'arquillian-junit', version: '1.0.0-SNAPSHOT'   
    jbossasRemoteTestRuntime group: 'org.jboss.arquillian.container', name: 'arquillian-jbossas-remote-6', version: '1.0.0-SNAPSHOT'
    jbossasRemoteTestRuntime group: 'org.jboss.jbossas', name: 'jboss-as-server', classifier: 'client', version: '6.1.0.Final', transitive: false
    jbossasRemoteTestRuntime group: 'org.jboss.jbossas', name: 'jboss-as-profileservice', classifier: 'client', version: '6.1.0.Final'
}

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

eclipse {
    classpath {
       downloadSources=true
       plusConfigurations += configurations.seleniumCompile
    }
}

task selenium(type: Test) {
   testClassesDir = sourceSets.selenium.output.classesDir
   classpath = sourceSets.selenium.runtimeClasspath  + files('src/selenium/resources-jbossas') + configurations.jbossasRemoteTestRuntime
}

我使用gradlew selenium从命令行运行 selenium 测试。
请告知为什么日志不工作无论是调试或信息.

vpfxa7rd

vpfxa7rd1#

可能的原因有多种:

  • 你的log4j.properties不在类路径上。打印出类路径以确保www.example.com所在的目录log4j.properties出现在那里。如果log4j没有找到任何配置,你应该在控制台看到一个警告。
  • 使用了一些其他的log4j.properties。查看默认的初始化过程here,了解可能会考虑哪些文件。由于log4j最有可能使用类加载器来查找配置文件,因此应该可以配置java,使其记录类加载器加载的所有内容。这将是很多东西,但它也应该包含log4j.properties文件的使用。或者搜索log4j源代码中的“log4j.properties”,使用它来查找加载配置的位置,然后使用调试器来查找加载的具体内容。
zbsbpyhn

zbsbpyhn2#

尝试重建项目。我用这种方法解决了问题。

pkwftd7m

pkwftd7m3#

尝试将此添加到build.gradle

applicationDefaultJvmArgs = ["-Dlog4j.configurationFile=log4j.properties"]

test {
    jvmArgs "-Dlog4j.configurationFile=log4j.properties"
}

我的log4j.properties文件位于src/main/resources中。

相关问题