如何在webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT情况下使用固定端口?

huangapple go评论60阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月10日 20:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定