Skip to content

Commit 37456ae

Browse files
barrycomminsrnorth
authored andcommitted
Fixed getServicePort on DockerComposeContainer throws NullPointerException if service instance number in not used. (#619)
1 parent 56ffbfd commit 37456ae

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
### Fixed
77
- Fixed extraneous insertion of `useSSL=false` in all JDBC URL strings, even for DBs that do not understand it. Usage is now restricted to MySQL by default and can be overridden by authors of `JdbcDatabaseContainer` subclasses ([\#568](https://github.com/testcontainers/testcontainers-java/issues/568))
8+
- Fixed `getServicePort` on `DockerComposeContainer` throws NullPointerException if service instance number in not used. ([\#619](https://github.com/testcontainers/testcontainers-java/issues/619))
89

910
### Changed
1011
- Abstracted and changed database init script functionality to support use of SQL-like scripts with non-JDBC connections. ([\#551](https://github.com/testcontainers/testcontainers-java/pull/551))

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public String getServiceHost(String serviceName, Integer servicePort) {
352352
* @return a port that can be used for accessing the service container.
353353
*/
354354
public Integer getServicePort(String serviceName, Integer servicePort) {
355-
return ambassadorContainer.getMappedPort(ambassadorPortMappings.get(serviceName).get(servicePort));
355+
return ambassadorContainer.getMappedPort(ambassadorPortMappings.get(getServiceInstanceName(serviceName)).get(servicePort));
356356
}
357357

358358
public SELF withScaledService(String serviceBaseName, int numInstances) {

core/src/test/java/org/testcontainers/junit/DockerComposeContainerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.testcontainers.junit;
22

33
import org.junit.Rule;
4+
import org.junit.Test;
45
import org.testcontainers.containers.DockerComposeContainer;
56

67
import java.io.File;
78

9+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
10+
import static org.rnorth.visibleassertions.VisibleAssertions.assertNotNull;
11+
812
/**
913
* Created by rnorth on 08/08/2015.
1014
*/
@@ -20,4 +24,13 @@ public class DockerComposeContainerTest extends BaseDockerComposeTest {
2024
protected DockerComposeContainer getEnvironment() {
2125
return environment;
2226
}
27+
28+
@Test
29+
public void testGetServicePort() {
30+
int serviceWithInstancePort = environment.getServicePort("redis_1", REDIS_PORT);
31+
assertNotNull("Port is set for service with instance number", serviceWithInstancePort);
32+
int serviceWithoutInstancePort = environment.getServicePort("redis", REDIS_PORT);
33+
assertNotNull("Port is set for service with instance number", serviceWithoutInstancePort);
34+
assertEquals("Service ports are the same", serviceWithInstancePort, serviceWithoutInstancePort);
35+
}
2336
}

0 commit comments

Comments
 (0)