Reputation: 29
The problem is that when I build my Spring project, which contains the integrational tests, with docker, I get a date parsing error. Without using the mvn clean package
inside docker, all tests run correctly on my PC. But using docker, when running tests, a date parsing error is displayed, since the docker container runtime provides a date with high accuracy.
error:
app_1 | Caused by: org.testcontainers.shaded.com.fasterxml.jackson.databind.exc.InvalidFormatException:
app_1 | Can not deserialize value of type java.util.Date from String "2025-02-10T22:52:16.598666264+03:00": not a valid representation (error: Failed to parse Date value '2025-02-10T22:52:16.598666264+03:00': Can not parse date "2025-02-10T22:52:16.598666264+0300": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
app_1 | at [Source: N/A; line: -1, column: -1] (through reference chain: com.github.dockerjava.api.model.Network["Created"])
docker-compose:
services:
app:
build: ./TechnicalTask_Boot
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
postgres:
condition: service_healthy
cassandra:
condition: service_healthy
ports:
- 8080:8080
postgres:
build: ./databases/Postgres
volumes:
- postgresdb:/var/lib/postgresql
healthcheck:
test:
- CMD-SHELL
- |
psql -U postgres -d GameData_db -tc \
"SELECT 1 FROM information_schema.tables \
WHERE table_name = 'GameData_db';"
interval: 1s
timeout: 3s
retries: 5
cassandra:
build: ./databases/Cassandra
volumes:
- cassandradb:/var/lib/cassandra
healthcheck:
test:
- CMD-SHELL
- |
cqlsh -e "DESCRIBE KEYSPACES;" > /dev/null 2>&1 || exit 1
interval: 1s
timeout: 3s
retries: 60
volumes:
postgresdb:
cassandradb:
image:
FROM openjdk:8-slim
ENV MAVEN_VERSION=3.8.1
ENV POSTGRES_PASSWORD=5e-Jo87_hhNf*9
ENV POSTGRES_USERNAME=postgres
ENV POSTGRES_HOST=postgres
RUN apt-get update && apt-get install -y wget tar docker.io \
&& wget -O /tmp/apache-maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& tar xzvf /tmp/apache-maven.tar.gz -C /opt \
&& ln -s /opt/apache-maven-${MAVEN_VERSION} /opt/maven \
&& rm /tmp/apache-maven.tar.gz
ENV MAVEN_HOME=/opt/maven
ENV PATH=${MAVEN_HOME}/bin:${PATH}
WORKDIR /app
COPY . .
ENTRYPOINT ["sh", "-c", "mvn clean package && java- jar /app/target/TechnicalTask_Boot-1.0-SNAPSHOT.jar"]
test class:
@Testcontainers
@SpringBootTest(properties = {
"spring.liquibase.enabled=false",
"spring.jpa.hibernate.ddl-auto=update",
"spring.profiles.active="
}, classes = {TechnicalTaskBootApplication.class})
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@PropertySource("classpath:application-test.properties")
public class GameDataIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Container
private static final PostgreSQLContainer<?> postgreSQLContainer =
new PostgreSQLContainer<>("postgres:13.2");
@Container
@SuppressWarnings("resource")
private static final CassandraContainer<?> cassandraContainer =
new CassandraContainer<>("cassandra:4.0.15")
.withExposedPorts(9042)
.withInitScript("testcontainers-migration/cassandra/schema.cql");
@DynamicPropertySource
static void configureCassandraProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.cassandra.contact-points",
() -> cassandraContainer.getHost() + ":" + cassandraContainer.getMappedPort(9042));
registry.add("spring.data.cassandra.keyspace-name", () -> "user_data_keyspace");
registry.add("spring.data.cassandra.local-datacenter", () -> "datacenter1");
registry.add("spring.data.cassandra.username", cassandraContainer::getUsername);
registry.add("spring.data.cassandra.password", cassandraContainer::getPassword);
}
@DynamicPropertySource
static void configurePostgreSQLProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.jpa.generate-ddl", () -> true);
}
}
configuration class:
@SpringBootApplication
public class TechnicalTaskBootApplication {
public static void main(String[] args) {
SpringApplication.run(TechnicalTaskBootApplication.class, args);
}
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.registerModule(new JavaTimeModule())
.setDateFormat(new StdDateFormat().withLenient(false));
}
}
Upvotes: 0
Views: 34