GithubHelp home page GithubHelp logo

Comments (2)

quaff avatar quaff commented on July 30, 2024 1

You can try annotating @org.testcontainers.junit.jupiter.Testcontainers on class WithContainers.

But it will trigger another problem, the @DynamicPropertySource method is invoked twice from DynamicPropertiesContextCustomizer and DynamicPropertySourceMethodsImporter.

Thread [main] (Suspended (breakpoint at line 22 in TestContainerTests))	
	owns: DefaultContextCache  (id=82)	
	TestContainerTests.populateDynamicPropertyRegistry(DynamicPropertyRegistry) line: 22	
	NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
	NativeMethodAccessorImpl.invoke(Object, Object[]) line: 77	
	DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43	
	Method.invoke(Object, Object...) line: 568	
	ReflectionUtils.invokeMethod(Method, Object, Object...) line: 281	
	DynamicPropertiesContextCustomizer.lambda$buildDynamicPropertiesMap$3(DynamicPropertyRegistry, Method) line: 82	
Thread [main] (Suspended (breakpoint at line 22 in TestContainerTests))	
	owns: DefaultContextCache  (id=82)	
	TestContainerTests.populateDynamicPropertyRegistry(DynamicPropertyRegistry) line: 22	
	NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
	NativeMethodAccessorImpl.invoke(Object, Object[]) line: 77	
	DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43	
	Method.invoke(Object, Object...) line: 568	
	ReflectionUtils.invokeMethod(Method, Object, Object...) line: 281	
	DynamicPropertySourceMethodsImporter.lambda$registerDynamicPropertySources$0(DynamicPropertyRegistry, Method) line: 57	
	0x000000012f319850.accept(Object) line: not available	

Here is the test case:

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@SpringJUnitConfig
//@Testcontainers
public class TestContainerTests {

	@Container
	static PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres");

	@DynamicPropertySource
	static void populateDynamicPropertyRegistry(DynamicPropertyRegistry registry) {
		System.out.println(container.getMappedPort(5432));
	}

	@Test
	void test() {
	}

	@Configuration
	@ImportTestcontainers(TestContainerTests.class)
	static class Config {

	}

}

from spring-boot.

wilkinsona avatar wilkinsona commented on July 30, 2024 1

The problem is that you're accessing the container directly within your @DynamicPropertySource method:

println(localStackContainer.getEndpointOverride(SQS))

This requires the container to have been started when the @DynamicPropertySource method is called, undoing the benefit of the registry's lazy nature where you register a property with a supplier for its value. Using a supplier means that the container does not have to have been started until the property is accessed which happens later in the application lifecycle.

This worked in 3.2.0 as the containers were started very early but that was at the expense of support for parallel startup being broken. This was fixed in #38831.

In summary, this only worked in 3.2.0 accidentally and, unfortunately, you'll have to make some changes to adapt to the way things now work. You can either remove println calls that access the container or move them into the supplier implementations where the values for the properties are being returned.

from spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.