testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

feat: add getMappedPort(int, InternetProtocol) overload to ContainerState

Open suryateja-g13 opened this issue 6 months ago • 0 comments

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 any InternetProtocol (TCP or UDP).
  • Reimplemented getMappedPort(int) to delegate to the new overload with InternetProtocol.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 — verifies IllegalArgumentException is thrown for an unmapped UDP port, with the port/protocol in the message.

All 9 tests in ContainerStateTest pass.

suryateja-g13 avatar May 26 '26 02:05 suryateja-g13