I'm trying to put traces into a Spock test to see how the PostgreSQL container was created by the testcontainer. But neither with log4j , nor with prints on the standard output nor by extending the Spec with a routine I can do it. The test code is as follows::
package com.makobrothers.mako.rrhh.persona.infrastructure.controller
import com.makobrothers.mako.IntegrationTest
import com.makobrothers.mako.MakoApplication
import org.junit.experimental.categories.Category
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.TestPropertySource
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
import spock.lang.Shared;
import spock.lang.Specification
import java.text.DateFormat
import java.text.SimpleDateFormat
import static org.hamcrest.Matchers.*
import org.json.simple.JSONObject
@ActiveProfiles("test")
@Testcontainers
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = MakoApplication.class
)
@TestPropertySource(locations = "classpath:application-test.yml")
@Category(IntegrationTest.class)
//@Sql(scripts = ["classpath:/createTablePersona.sql", "classpath:/insertTablePersona.sql"])
class PersonaRestAssuredSpec extends Specification {
final static Logger log = LoggerFactory.getLogger(PersonaRestAssuredSpec.class)
final static POSTGRES_TEST_IMAGE = "postgres:15.1"
final static Random RANDOM = new Random();
public static PostgreSQLContainer staticPostgreSQL = new PostgreSQLContainer(POSTGRES_TEST_IMAGE)
.withDatabaseName("makoTest")
.withUsername("mako")
.withPassword("mako")
.withExposedPorts(5432)
.withCreateContainerCmdModifier({ cmd -> cmd.withName('postgreSQL-' ( RANDOM.nextInt() & Integer.MAX_VALUE )) })
@Shared
public PostgreSQLContainer postgreSQLContainer = staticPostgreSQL
@Value('\${server.http.port}')
private int port
def setup() throws Exception {
RestAssured.port = port
log.info("Url: " postgreSQLContainer.getJdbcUrl())
log.warn("Url: " postgreSQLContainer.getJdbcUrl())
log.error("Url: " postgreSQLContainer.getJdbcUrl())
showTrace "Url: " postgreSQLContainer.getJdbcUrl()
showTrace "DatabaseName: " postgreSQLContainer.getDatabaseName()
showTrace "Username: " postgreSQLContainer.getUsername()
showTrace "Password: " postgreSQLContainer.getPassword()
showTrace "Port: " postgreSQLContainer.getBoundPortNumbers()
}
def "showConfigTest"() {
given: "A client request to test findById"
log.info("Url: " postgreSQLContainer.getJdbcUrl())
log.warn("Url: " postgreSQLContainer.getJdbcUrl())
log.error("Url: " postgreSQLContainer.getJdbcUrl())
showTrace "Url: " postgreSQLContainer.getJdbcUrl()
int left = 2
int right = 2
when: "Nothing"
int result = left right
then: "Nothing"
assert result == 4
}
}
I have extended Spec with a method to print (SpockConfig.groovy) as I saw an example in another query:
import spock.lang.Specification
class LabelPrinter {
def showTrace(def message) {
println message
true
}
}
Specification.mixin LabelPrinter
And the gradle part is declared with:
test {
useJUnitPlatform()
testLogging {
events "PASSED", "SKIPPED", "FAILED", "STANDARD_OUT", "STANDARD_ERROR"
}
testClassesDirs = sourceSets.test.output
classpath = sourceSets.test.runtimeClasspath
reports {
html.enabled = true
}
}
The output is allways the same:
$ gradle clean test
> Task :test
com.makobrothers.mako.rrhh.persona.infrastructure.controller.PersonaRestAssuredSpec
✔ showConfigTest (5.5s)
1 passing (1m 15s)
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1m 27s
6 actionable tasks: 6 executed
CodePudding user response:
Added this to gradle and now it works:
testlogger {
// pick a theme - mocha, standard, plain, mocha-parallel, standard-parallel or plain-parallel
theme 'mocha'
// set to false to disable detailed failure logs
showExceptions true
// set threshold in milliseconds to highlight slow tests
slowThreshold 2000
// displays a breakdown of passes, failures and skips along with total duration
showSummary true
// set to false to hide passed tests
showPassed true
// set to false to hide skipped tests
showSkipped true
// set to false to hide failed tests
showFailed true
// enable to see standard out and error streams inline with the test results
showStandardStreams true
// set to false to hide passed standard out and error streams
showPassedStandardStreams true
// set to false to hide skipped standard out and error streams
showSkippedStandardStreams true
// set to false to hide failed standard out and error streams
showFailedStandardStreams true
}
test {
useJUnitPlatform()
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
events "PASSED", "SKIPPED", "FAILED", "STANDARD_OUT", "STANDARD_ERROR"
}
testClassesDirs = sourceSets.test.output
classpath = sourceSets.test.runtimeClasspath
reports {
html.enabled = true
}
}
I think it is the showStandardStreams property that allows the logs to be seen.