Port for @StartStop annotation
Closed this issue · 4 comments
Please provide the ability to start the mock server on a predefined port, like:
@StartStop
public final MockWebServer server = new MockWebServer(9999);
Otherwise, older code like cannot be replaced with the new annotation.
@BeforeEach
public void setup() {
this.server = new MockWebServer();
server.start(9999);
}
There are testcases where the port must be known before startup, and cannot be derived dynamically for the tests. Therefore, it is necessary from time to time to set the port explicit.
There are testcases where the port must be known before startup
Can you give an example?
Of course: I integrate the [splunk-library-javalogging](https://github.com/splunk/splunk-library-javalogging) library.
To test the logging works correctly in my application, I run a MockWebServer to verify the requests are sent out correctly to the splunk-server.
The logger requires the url and port as follows:
/src/main/resources/log4j2-splunk-appender.xml:
<SplunkHttp
name="SPLUNK"
url="${spring:app.splunk.url}"
.../>
</SplunkHttp>
The url can be hardcoded, but also come from a application.properties like:
/src/test/resources/application.properties:
app.splunk.url=http://localhost:9999
As the log4j2 logger is initialized before spring application context, the port can neither be dynamically derived, nor injected.
For tests, the port has to be predefined, and thus the MockWebServer must be runnable on a fixed port, like is was in v4:
@BeforeEach
public void setup() {
this.server = new MockWebServer();
server.start(9999);
}
That makes sense.
You can still start it yourself explicitly, you don't have to use StartStop. The existing APIs should work if the classpath is consistent.
So I'm not sure it's worth adding a port here. The testing case is usually best with ephemeral ports.
Yeah I’d recommend just doing your own start() and stop() manually in this case. It’ll be awkward for us to have a configurable port with our current implementation!