testcontainers-java
testcontainers-java copied to clipboard
feat: add getMappedPort(int, InternetProtocol) overload to ContainerState
Summary
Closes #554
The existing getMappedPort(int) always performs a TCP lookup. There is no typed API to retrieve the mapped port for a UDP-exposed container port — callers must walk the full getContainerInfo() chain manually, as noted in the original issue.
Changes
ContainerState
- Added
getMappedPort(int originalPort, InternetProtocol protocol)— looks up the mapped host port for anyInternetProtocol(TCP or UDP). - Reimplemented
getMappedPort(int)to delegate to the new overload withInternetProtocol.TCP, so both paths share the same logic. - Error message now includes the protocol (e.g.
"Requested port (53/udp) is not mapped").
Tests
Added three unit tests to ContainerStateTest (no Docker required, Mockito-only):
getMappedPortWithTcpProtocolReturnsMappedPort— verifies TCP lookup returns the correct mapped port via both the no-arg and protocol overloads.getMappedPortWithUdpProtocolReturnsMappedPort— verifies UDP port lookup returns the correct mapped port.getMappedPortThrowsWhenPortNotMapped— verifiesIllegalArgumentExceptionis thrown for an unmapped UDP port, with the port/protocol in the message.
All 9 tests in ContainerStateTest pass.