我正在尝试将 Neo4j TestContainers 与 Kotlin、Spring Data Neo4j、Spring Boot 和 JUnit 5 一起使用。我有很多测试需要使用测试容器。理想情况下,我希望避免在每个测试类中复制容器定义和配置。
目前我有类似的东西:
@Testcontainers
@DataNeo4jTest
@Import(Neo4jConfiguration::class, Neo4jTestConfiguration::class)
class ContainerTest(@Autowired private val repository: XYZRepository) {
companion object {
const val IMAGE_NAME = "neo4j"
const val TAG_NAME = "3.5.5"
@Container
@JvmStatic
val databaseServer: KtNeo4jContainer = KtNeo4jContainer("$IMAGE_NAME:$TAG_NAME")
.withoutAuthentication()
}
@TestConfiguration
internal class Config {
@Bean
fun configuration(): Configuration = Configuration.Builder()
.uri(databaseServer.getBoltUrl())
.build()
}
@Test
@DisplayName("Create xyz")
fun testCreateXYZ() {
// ...
}
}
class KtNeo4jContainer(val imageName: String) : Neo4jContainer<KtNeo4jContainer>(imageName)
Run Code Online (Sandbox Code Playgroud)
如何提取数据库服务器定义和@TestConfiguration?我尝试了不同的方法来创建基类并让 ContainerTest 扩展它,但它不起作用。据我了解,静态属性在 Kotlin …
本地环境:
我在应用程序中使用 Elastic 搜索,并且我希望能够在我的集成测试中使用 TestContainers。使用 ElasticSearch testcontainer 的 Play Framework 应用中的示例代码:
@BeforeAll
public static void setup() {
private static final ElasticsearchContainer ES = new ElasticsearchContainer();
ES.start();
}
Run Code Online (Sandbox Code Playgroud)
这在本地测试时有效,但我希望能够在 Docker 容器中运行它以在我的 CI 服务器上运行。在 Docker 容器内运行测试时出现此异常:
[warn] o.t.u.RegistryAuthLocator - Failure when attempting to lookup auth config (dockerImageName: alpine:3.5, configFile: /root/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /root/.docker/config.json (No such file or directory)
[warn] o.t.u.RegistryAuthLocator - Failure when attempting …Run Code Online (Sandbox Code Playgroud) 我在 Spring boot 应用程序中有一套集成测试。测试上下文使用 MSSQL docker 容器作为其数据库,并使用testcontainers框架。
我的一些测试使用 Mockito 和 SpyBean,这显然是有意设计的,它将重新启动 Spring 上下文,因为监视的 bean 不能在测试之间共享。
由于我使用的是在所有测试期间都存在的非嵌入式数据库,因此通过在开始时执行我的 schema.sql 和 data.sql 来配置数据库:-
spring.datasource.initialization-mode=always
Run Code Online (Sandbox Code Playgroud)
问题是,当 Spring 上下文重新启动时,我的数据库再次重新初始化,这会触发诸如唯一约束问题、表已存在等错误。
我的父测试类如下(如果有帮助的话):-
@ActiveProfiles(Profiles.PROFILE_TEST)
@Testcontainers
@SpringJUnitWebConfig
@AutoConfigureMockMvc
@SpringBootTest(classes = Application.class)
@ContextConfiguration(initializers = {IntegrationTest.Initializer.class})
public abstract class IntegrationTest {
private static final MSSQLServerContainer<?> mssqlContainer;
static {
mssqlContainer = new MSSQLServerContainer<>()
.withInitScript("setup.sql"); //Creates users/permissions etc
mssqlContainer.start();
}
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(final ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.datasource.url=" + mssqlContainer.getJdbcUrl())
.applyTo(configurableApplicationContext.getEnvironment()); …Run Code Online (Sandbox Code Playgroud) 尝试测试容器进行集成测试。我正在测试 rest api 端点。这里是技术栈——quarkus、RESTEasy 和 mongodb-client
我能够看到 MongoDB 容器已成功启动但出现异常。异常:“com.mongodb.MongoSocketOpenException:异常打开套接字”
2020-04-26 15:13:18,330 INFO [org.tes.doc.DockerClientProviderStrategy] (main) Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
2020-04-26 15:13:19,109 INFO [org.tes.doc.UnixSocketClientProviderStrategy] (main) Accessing docker with local Unix socket
2020-04-26 15:13:19,109 INFO [org.tes.doc.DockerClientProviderStrategy] (main) Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
2020-04-26 15:13:19,258 INFO [org.tes.DockerClientFactory] (main) Docker host IP address is localhost
2020-04-26 15:13:19,305 INFO [org.tes.DockerClientFactory] (main) Connected to docker:
Server Version: 19.03.8
API Version: 1.40
Operating System: Docker Desktop
Total Memory: 3940 MB
2020-04-26 …Run Code Online (Sandbox Code Playgroud) 我有一个名为 myImageName:latest 的 docker 映像,我在应用程序中使用它。我想为我的应用程序编写集成测试,因此我想使用 TestContainer 包。当我运行以下简单测试时:
@Testcontainers
public class myIntegrationTest{
@Container
private GenericContainer container =
new GenericContainer<>(DockerImageName.parse("myImageName:latest"))
.withExposedPorts(8080);
@Test
public void myIntegrationTestName() throws Exception{
assertTrue(container.isRunning());
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误(完整堆栈跟踪):
18:30:58.741 [main] 错误 org.testcontainers.dockerclient.DockerClientProviderStrategy - 找不到有效的 Docker 环境。请检查配置。尝试的配置是:2 18:30:58.746 [主要]错误org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy:失败,异常NoClassDefFoundError(无法初始化类org.testcontainers.shaded.com.github.dockerjava.core.DefaultObjectMapperHolder)18 :30:58.747 [main]错误org.testcontainers.dockerclient.DockerClientProviderStrategy - 由于未找到有效配置,因此无法继续执行
java.lang.IllegalStateException:找不到有效的 Docker 环境。请查看日志并检查org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$7(DockerClientProviderStrategy.java:215) at java.base/java.util.Optional.orElseThrow(Optional.java:408) at org.testcontainers的配置。 dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:207) 在 org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:136) 在 org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:178) 在 org.testcontainers.LazyDockerClient.getDockerClient (LazyDockerClient.java:14) 在 org.testcontainers.LazyDockerClient.authConfig(LazyDockerClient.java:12) 在 org.testcontainers.containers.GenericContainer.start(GenericContainer.java:310) 在 org.testcontainers.junit.jupiter.TestcontainersExtension$ StoreAdapter.start(TestcontainersExtension.java:242) 在 org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.access$200(TestcontainersExtension.java:229) 在 org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$null$4(TestcontainersExtension. java:82) 在 org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$4(ExtensionValuesStore.java:86) 在 org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:205 )在 org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.evaluate(ExtensionValuesStore.java:182) 在 org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.access$100(ExtensionValuesStore.java:171) …
我在机器上运行应用程序JUnit5的集成测试(使用)时遇到问题。我的测试依赖于两个容器,我使用Testcontainers java 库定义:和。版本是.micronautWindows 10 ProRabbitMQMS SQL serverDockerv19.03.13
运行集成测试后,我收到以下异常:
\n[WARN ][DESKTOP-PBCHP60] [testcontainers.utility.TestcontainersConfiguration] \xe2\x80\x93 Attempted to read Testcontainers configuration file at file:/$HOME.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: $HOME\\.testcontainers.properties (The system cannot find the file specified)\nRun Code Online (Sandbox Code Playgroud)\n不过,从机器运行测试时我没有收到此异常macOS。我的假设是它是Windows特定的。
谁能告诉我如何解决它?我应该自己创建所需的文件吗?或者也许有一种方法可以在运行 IT 期间跳过读取此文件的步骤?
\n我有两个 spring boot 项目,它们都具有相同的 JDK 并且在同一台机器上运行(apple m1)。第一个项目只是一个虚拟项目,其中仅包含 testcontainer 依赖项。
虽然第二个项目是一个遗留项目,我应该在其中集成测试容器。
但是,当 testcontainer 在第一个项目中运行时,对于第二个项目,我收到以下错误(我刚刚从第一个项目复制粘贴了 testcontainer 代码)
JDK:azul 15.0.5
JAVA 路径:Library/Java/JavaVirtualMachines/azul-15.0.5/Contents/Home/bin/java Docker 桌面版本:4.5.0
如何解决这个错误?
17:15:41.181 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
17:15:41.189 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
17:15:41.211 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.housing.cron.ListingUpdateBatchContainerTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
17:15:41.218 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.housing.cron.ListingUpdateBatchContainerTest], using SpringBootContextLoader
17:15:41.221 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not …Run Code Online (Sandbox Code Playgroud) 我有一个java项目,它使用testcontainers进行集成测试。我想在该阶段实现 gitlab ci 但我遇到了这个错误
\njava.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration\nRun Code Online (Sandbox Code Playgroud)\n项目在本地计算机上成功构建,但 gitlap ci docker 无法正确启动
\n这里有一个日志输出
\n\n[0KRunning with gitlab-runner 14.3.0 (b37d3da9)[0;m\n[0K on Backend Docker runner 9L3Zko1w[0;m\nsection_start:1657698628:prepare_executor\n[0K[0K[36;1mPreparing the "docker" executor[0;m[0;m\n[0KUsing Docker executor with image maven:3.6.0-jdk-11-slim ...[0;m\n[0KStarting service docker:dind ...[0;m\n[0KPulling docker image docker:dind ...[0;m\n[0KUsing docker image sha256:232342342342 for docker:dind with digest docker@sha256:2342342342 ...[0;m\n[0KWaiting for services to be up and running...[0;m\n\n[0;33m*** WARNING:[0;m Service runner-23423423-docker-0 probably didn't start properly.\n\nHealth check error:\nservice "runner-234234234-docker-0-wait-for-service" …Run Code Online (Sandbox Code Playgroud) 我有一个 API REST .NET 7,我想使用 sql server 容器数据库构建集成测试。我尝试遵循文档中的示例(https://dotnet.testcontainers.org/examples/aspnet/):
const string weatherForecastStorage = "weatherForecastStorage";
var mssqlConfiguration = new MsSqlTestcontainerConfiguration();
mssqlConfiguration.Password = Guid.NewGuid().ToString("D");
mssqlConfiguration.Database = Guid.NewGuid().ToString("D");
var connectionString = $"server={weatherForecastStorage};user id=sa;password={mssqlConfiguration.Password};database={mssqlConfiguration.Database}";
_weatherForecastNetwork = new NetworkBuilder()
.WithName(Guid.NewGuid().ToString("D"))
.Build();
_mssqlContainer = new ContainerBuilder<MsSqlTestcontainer>()
.WithDatabase(mssqlConfiguration)
.WithNetwork(_weatherForecastNetwork)
.WithNetworkAliases(weatherForecastStorage)
.Build();
Run Code Online (Sandbox Code Playgroud)
但 MsSqlTestcontainer 和 MsSqlTestcontainerConfiguration 类不存在。
nugget 包版本是 3.0.0,但官方文档似乎已经过时,因为构造函数 ContainerBuilder<> 也已过时。
我正在寻找的是创建一个带有 SQL SERVER 映像的容器,然后创建一个数据库/表并将其与实体框架核心一起使用(我不知道如何创建它们,甚至不知道这是否有必要)。
另一方面,我尝试了这样的事情:
private readonly IContainer _dbContainer = new ContainerBuilder()
.WithImage("mcr.microsoft.com/mssql/server")
.WithEnvironment("MSSQL_SA_PASSWORD", "MyStrongPassword")
.WithEnvironment("ACCEPT_EULA", "Y")
.WithEnvironment("MSSQL_DATA_DIR", "/var/opt/sqlserver/data")
.WithEnvironment("MSSQL_LOG_DIR", "/var/opt/sqlserver/log")
.WithEnvironment("MSSQL_BACKUP_DIR", "/var/opt/sqlserver/backup")
.WithPortBinding(49401, 1433) …Run Code Online (Sandbox Code Playgroud) 我想在网络上运行一个带有 testcontainers 的容器。我的容器包含一个 spring 应用程序,它有一个执行器端点来表达其状态,它位于:
/actuator/health
Run Code Online (Sandbox Code Playgroud)
我的容器如下所示:
private final static Network network = Network.newNetwork();
private static final GenericContainer<?> myContainer = new GenericContainer<>("mycontainer:latest")
.withExposedPorts(8443)
.withNetwork(network)
.withNetworkAliases("myContainer")
.withClasspathResourceMapping("certs", "/app/certs", BindMode.READ_ONLY)
.withClasspathResourceMapping("config", "/app/config", BindMode.READ_ONLY)
.withLogConsumer(new Slf4jLogConsumer(log))
.waitingFor(Wait.forHttp("/actuator/health").usingTls());
Run Code Online (Sandbox Code Playgroud)
但是当我启动这个容器时,出现以下错误:
Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for URL to be accessible (https://localhost:33092/actuator/health should return HTTP [200])
at org.testcontainers.containers.wait.strategy.HttpWaitStrategy.waitUntilReady(HttpWaitStrategy.java:214)
at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:35)
at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:890)
at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:441)
... 68 more
Run Code Online (Sandbox Code Playgroud)
但是,如果我在https://localhost:33092/actuator/health容器内的应用程序启动后但正在等待评估时卷曲此网址(在本例中:),我会得到如下内容:
HTTP Status Code: 200
{
"status": "UP"
}
Run Code Online (Sandbox Code Playgroud)
我缺少什么?有任何想法吗?这是一个错误吗?