{"id":"0e22fc234b61e5af","repo":"junit-team/junit5","slug":"failed-to-connect-to-socket-on-port","errorCode":null,"errorMessage":"Failed to connect to socket on port ","messagePattern":"Failed to connect to socket on port ","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-platform-reporting/src/main/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListener.java","lineNumber":158,"sourceCode":"\t\t\t\treportInfrastructure(config);\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new JUnitException(\"Failed to initialize XML events writer\", e);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate DocumentWriter<Events> createDocumentWriter(ConfigurationParameters config,\n\t\t\tNamespaceRegistry namespaceRegistry) throws Exception {\n\t\treturn config.get(SOCKET_PROPERTY_NAME, Integer::valueOf) //\n\t\t\t\t.map(port -> {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tSocket socket = new Socket(InetAddress.getLoopbackAddress(), port);\n\t\t\t\t\t\tWriter writer = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8);\n\t\t\t\t\t\treturn Events.createDocumentWriter(namespaceRegistry, writer);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (Exception e) {\n\t\t\t\t\t\tthrow new JUnitException(\"Failed to connect to socket on port \" + port, e);\n\t\t\t\t\t}\n\t\t\t\t}) //\n\t\t\t\t.orElseGet(() -> {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tPath eventsXml = requireNonNull(outputDir).resolve(\"open-test-report.xml\");\n\t\t\t\t\t\treturn Events.createDocumentWriter(namespaceRegistry, eventsXml);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (Exception e) {\n\t\t\t\t\t\tthrow new JUnitException(\"Failed to create XML events file\", e);\n\t\t\t\t\t}\n\t\t\t\t});\n\t}\n\n\tprivate boolean isEnabled(ConfigurationParameters config) {\n\t\treturn config.getBoolean(ENABLED_PROPERTY_NAME).orElse(false);\n\t}\n\n\tprivate boolean isGitEnabled(ConfigurationParameters config) {","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-reporting/src/main/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListener.java#L140-L176","documentation":"Thrown by OpenTestReportGeneratingListener.createDocumentWriter() when the SOCKET_PROPERTY_NAME (junit.platform.reporting.open.xml.socket) is set to a port but opening a Socket to InetAddress.getLoopbackAddress() on that port fails. Wrapped as a JUnitException naming the port. Only reached when XML reporting is enabled and the socket property is present.","triggerScenarios":"Setting junit.platform.reporting.open.xml.socket=<port> with XML reporting enabled, when no server is listening on loopback at that port, the port is invalid, or the connection is refused.","commonSituations":"Starting the launcher before the report-consuming socket server is up. Wrong port number. Server bound to a non-loopback address (the listener only connects to loopback). Firewall blocking loopback. Test orchestration race between listener and server.","solutions":["Ensure the report server is listening on 127.0.0.1 at the configured port before tests start.","Double-check the port value in junit.platform.reporting.open.xml.socket.","If you do not want socket-based reporting, remove the property to fall back to file output.","Bind the server to loopback since the listener uses InetAddress.getLoopbackAddress()."],"exampleFix":"# before\njunit.platform.reporting.open.xml.socket = 12345\n# nothing listening on 12345 -> JUnitException\n\n# after: start the server first, or drop socket mode\n# (remove the property to write open-test-report.xml to disk)","handlingStrategy":"validation","validationCode":"int port = Integer.parseInt(System.getProperty(\"junit.platform.reporting.open.xml.socket\"));\ntry (Socket s = new Socket(InetAddress.getLoopbackAddress(), port)) {\n    // reachable\n} catch (IOException e) {\n    throw new IllegalStateException(\"report socket server not listening on \" + port);\n}","typeGuard":"static boolean socketListening(int port) {\n    try (Socket s = new Socket(InetAddress.getLoopbackAddress(), port)) { return true; }\n    catch (IOException e) { return false; }\n}","tryCatchPattern":"try {\n    new Socket(InetAddress.getLoopbackAddress(), port);\n} catch (IOException e) {\n    // start the server first, or unset the socket property to use file output\n}","preventionTips":["Start the report server on loopback before launching tests.","Remove junit.platform.reporting.open.xml.socket to fall back to file output.","Bind the server to 127.0.0.1; the listener only connects to loopback."],"tags":["reporting","socket","network","configuration","junit-platform"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}