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:

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

    private int serverPort;

    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/