我有一个简单的Spring Boot(2.1.4
)应用程序,该应用程序使用JUnit5和H2进行Gradle测试。一切正常,每个测试都通过。但是,如果我在容器内运行相同的测试,它将失败。似乎H2不会在每个测试之间重置。令人讨厌的是,这仅在此特定测试中发生。
这是我的application-test.properties
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
# Enable web path to access H2 console
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.olap.datasource.driverClassName=org.h2.Driver
spring.olap.datasource.jdbc-url=jdbc:h2:mem:db1;MODE=Oracle;INIT=CREATE SCHEMA IF NOT EXISTS TEST_DW
spring.olap.datasource.url=jdbc:h2:mem:db1;MODE=Oracle;INIT=CREATE SCHEMA IF NOT EXISTS TEST_DW
spring.olap.datasource.username=sa
spring.olap.datasource.password=sa
这是我的考试
package com.test.repository.olap;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
class PurchaseRepositoryTest {
@Autowired
PurchaseRepository purchaseRepository;
@Test
void can_get_the_data() {
// I know this will fix the issue but why its necessary here inside Docker container and not outside.
// this.purchaseRepository.deleteAll();
assertFalse(this.purchaseRepository.findAll().iterator().hasNext());
}
}
这是我的Dockerfile
FROM openjdk:8-jdk-alpine as build
WORKDIR /workspace/app
COPY gradle gradle
#COPY .gradle .gradle
COPY build.gradle .
COPY src src
COPY gradlew gradlew
RUN ./gradlew build -DskipTests
RUN mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar)
FROM openjdk:8-jdk-alpine
ARG DEPENDENCY=/workspace/app/build/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","Test.Application"]
反正有解决此问题的方法。