英文:
How to use a fixed port with the webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT?
问题
I am working on an Integration Test, and I want the server to launch on the same port every time. I have set the value of webEnvironment to DEFINED_PORT in the @SpringBootTest annotation. However, a random port is still being accessed. How to fix the port value?
@SpringBootTest(
classes = AdsApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestExecutionListeners(
listeners = {
SpringBootDependencyInjectionTestExecutionListener.class,
ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class
})
@RunWith(RunIfRunnerExtended.class)
@RunIfExtended(LocalCheck.class)
public class AutoSpecUpdateIntegrationTest {
// @LocalServerPort
private int randomServerPort = 63547;
@Test
public void autoSpecUpdate() throws IOException {
String url = "http://localhost:" + randomServerPort + "/" + version + "/api-docs";
}
英文:
I am working on an Integration Test, and I want the server to launch on the same port every time. I have set the value of webEnvironment to DEFINED_PORT in the @SpringBootTest annotation. However, a random port is still being accessed. How to fix the port value?
@SpringBootTest(
classes = AdsApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestExecutionListeners(
listeners = {
SpringBootDependencyInjectionTestExecutionListener.class,
ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class
})
@RunWith(RunIfRunnerExtended.class)
@RunIfExtended(LocalCheck.class)
public class AutoSpecUpdateIntegrationTest {
// @LocalServerPort
private int randomServerPort = 63547;
@Test
public void autoSpecUpdate() throws IOException {
String url = "http://localhost:" + randomServerPort + "/" + version + "/api-docs";
}
答案1
得分: 0
"Along with webEnvironment you need to define property server.port
. It may fail after the first test case and throw the error Port 8090 is already in use
. To avoid that, you need to add @DirtiesContext
annotation.
@SpringBootTest(
webEnvironment = WebEnvironment.DEFINED_PORT,
properties = {
"server.port=8090"
})
@DirtiesContext
Now you can use @LocalServerPort
to get the port."
英文:
Along with webEnvironment you need to define property server.port
. It may fail after first test case and throw error Port 8090 is already in use
. To avoid that you need to add @DirtiesContext
annotation.
@SpringBootTest(
webEnvironment = WebEnvironment.DEFINED_PORT,
properties = {
"server.port=8090"
})
@DirtiesContext
Now you can use @LocalServerPort to get port.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论