using testcontainer; I'm trying to write an integration test for my project with kafka, postgreSql and elasticsearch, but when I run docker-compose_v2.yml file and run my tests, my tests are successful, but when I use testcontainer, my tests fail, I can't connect to kafka while the tests are running.
Asked
Active
Viewed 597 times
0
-
Please add your code as a [mcve] to the question, not external links. Also, your tests links are the exact same, so its not clear what is failing – OneCricketeer Sep 21 '22 at 22:15
-
Hi, I've corrected my docker-compose file to allow services to communicate with each other, not through static IP, but with the service name. In fact, before I asked this question, I tried to run my integration tests using the docker-compose module you shared with me, but what I didn't understand was, is that my application with the containers running by the test containers is transacting on the same network, so broker: 29092 or localhost: 9092, I couldn't decide which one of the following addresses to request – Semih Sep 22 '22 at 00:02
-
Where do your tests actually run? That decides which to connect to. https://stackoverflow.com/questions/51630260/connect-to-kafka-running-in-docker#51634499 – OneCricketeer Sep 22 '22 at 03:02
-
If you are asking if my tests are running in the cloud or running on my local computer, I run them on my local computer, but I do not know if my tests and test containers are running on the same network, I know that Kafka accepts requests from different ports if they are running on the same network or on a different network, but the answer to the above question I can't find a solution because I don't know – Semih Sep 22 '22 at 07:10
1 Answers
1
Docker Compose should not use IP addresses. You should use Service names directly for endpoint resolution.
Your Compose file is not attached to the TestContainer network when you use Network.newNetwork()
.
If you want to use Compose in your tests, then do that, not run individual GenericContainers - https://www.testcontainers.org/modules/docker_compose/

OneCricketeer
- 179,855
- 19
- 132
- 245
-
i've updated my project again and i've tried to run test container using docker-compose, but this way the tests can't connect to the skull while they're running. – Semih Sep 22 '22 at 00:21
-
logs => 2022-09-22 03:16:13.053 WARN 17564 --- [ntainer#5-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-driver-group-4, groupId=driver-group] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2022-09-22 03:16:13.053 WARN 17564 --- [ntainer#5-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-driver-group-4, groupId=driver-group] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected – Semih Sep 22 '22 at 00:25
-
test docker-compose file => https://github.com/semihshn/driver-service/blob/main/src/test/resources/test-compose.yml integration tests => https://github.com/semihshn/driver-service/blob/main/src/test/java/com/semihshn/driverservice/integration/driver/steps/RetrieveDriverByIdFeatureSteps.java – Semih Sep 22 '22 at 00:25
-
I cannot tell which of those tests actually uses Kafka container – OneCricketeer Sep 22 '22 at 03:03
-
Why do you need to know that? Does the testcontainer library correctly run external applications, such as Kafka, which my application uses? Were the Testcontainers properly carried out? – Semih Sep 22 '22 at 07:13
-
Refer [mcve]. If your question is related to Kafka, please show that, not some stack of 4+ interconnected services. From your integration test link, I see you using Elasticsearch and some HTTP requests, and nothing doing Kafka things – OneCricketeer Sep 24 '22 at 00:51