The webEnvironment OptionwebEnvironment option inside @SpringBootTest@SpringBootTest is very important. It can take values like NONENONE, MOCKMOCK, RANDOM_PORTRANDOM_PORT, DEFINED_PORT.
NONE will only create spring beans not any mock servlet environment.
MOCK will create spring beans and a mock servlet environmentDEFINED_PORT.
RANDOM_PORT will start the actual Servlet container on a random port, this can be autowired using the @LocalServerPort.
NONEwill only create spring beans and not any mock the servlet environment.MOCKwill create spring beans and a mock servlet environment.RANDOM_PORTwill start the actual servlet container on a random port; this can be autowired using the@LocalServerPort.DEFINED_PORTwill take the defined port in the properties and start the server with it.
DEFINED_PORT will take the defined port in the properties and start the server on them. TheThe default is RANDOM_PORTRANDOM_PORT when u dontyou don’t define any webEnvironmentwebEnvironment. So the app may be starting inat a different port for uyou.
Try to override it to DEFINED_PORT and tryDEFINED_PORT, or else try to Autowireautowire the port nonumber and try to run test on that port.