Reputation: 1208
I have spring boot application and use karate for testing. I used log4j2 for logging. Karate use logback as a log library. I found this link for basic configuration of log4j2 with karate. But unfortunately print statement in the feature file is not written to console.
This is my Simple test to print to console.
Scenario: Print to Console
* print 'Hello Word'
Log4j2.properties file
log4j.rootLogger = INFO, CONSOLE
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
As you see bellow I excluded logback dependecies from karate with this code
testImplementation('com.intuit.karate:karate-junit5:0.9.6') {
exclude module: "logback-classic"
}
testImplementation('com.intuit.karate:karate-apache:0.9.6') {
exclude module: "logback-classic"
}
Gradle build file
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id "org.openapi.generator" version "4.3.1"
//id 'war'
}
group = 'test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
implementation('org.springframework.boot:spring-boot-starter-data-jpa') {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
implementation('org.springframework.boot:spring-boot-starter-validation') {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
implementation('org.springframework.boot:spring-boot-starter-quartz'){
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
implementation("org.springframework.boot:spring-boot-starter-log4j2")
implementation 'org.mapstruct:mapstruct:1.4.0.Final'
implementation 'org.zalando:jackson-datatype-money:1.2.0'
compileOnly 'org.projectlombok:lombok'
implementation 'javax.validation:validation-api:2.0.0.Final'
implementation 'com.vladmihalcea:hibernate-types-52:2.9.13'
implementation group: 'org.openapitools', name: 'jackson-databind-nullable', version: '0.2.1'
implementation 'io.swagger:swagger-annotations:1.5.9'
implementation 'org.javamoney:moneta:1.4.2'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.0.Final'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation('com.intuit.karate:karate-junit5:0.9.6') {
exclude module: "logback-classic"
}
testImplementation('com.intuit.karate:karate-apache:0.9.6') {
exclude module: "logback-classic"
}
testCompile group: 'com.h2database', name: 'h2', version: '1.4.200'
}
test {
useJUnitPlatform()
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "test"
}
bootRun {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "dev"
}
sourceSets {
test {
resources {
srcDir file('src/test/java')
exclude '**/*.java'
}
}
}
Test Class:
import com.intuit.karate.junit5.Karate;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class RunAllKarateTest {
@LocalServerPort
private int port;
@BeforeEach
void setUp() {
System.getProperties().setProperty("server.port", String.valueOf(port));
}
@Karate.Test
Karate runAll() {
return Karate.run().relativeTo(getClass());
}
}
Karate Config
function fn() {
var port = karate.properties['server.port'];
if (!port) {
port = 8080;
}
var config = {
baseUrl : 'http://localhost:'+port,
uuid: function(){ return java.util.UUID.randomUUID() + '' },
isoDateTime: function(){ return java.time.OffsetDateTime.now().format(java.time.format.DateTimeFormatter.ISO_DATE_TIME)}
};
return config;
}
This is the report screenshot
Upvotes: 3
Views: 1281
Reputation: 1208
it start working after changing configuration of log4j2.properties files
New configuration as follows:
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout = org.apache.log4j.PatternLayout
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
Upvotes: 2