{"@id":"cedric-dumont.com"}

A developer's braindump

How to get the running port in springboot Tests

To get the random server port in spring boot tests, you must :

  • decorate your test class with : @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  • add a property in your Test class : @LocalServerPort private String serverPort;

full code snippet:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class RandomPortTest{

    @LocalServerPort
    private int serverPort;

    @Test
    public void validateRandomPort(){
       assertThat(serverPort > 0).isTrue();
    }
}
Next: /2019/01/11/using-multiple-accounts-on-the-same-machine-for-github/
Prev: /2017/07/04/migrate-svn-to-git-repo-from-a-local-svn-reposiroty/