Bird
0
0

Which Spring Boot test annotation setup is required to launch the application on a random port so that TestRestTemplate can perform real HTTP requests?

easy📝 Conceptual Q2 of 15
Spring Boot - Testing Spring Boot Applications
Which Spring Boot test annotation setup is required to launch the application on a random port so that TestRestTemplate can perform real HTTP requests?
A@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
B@WebMvcTest with @AutoConfigureMockMvc
C@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
D@DataJpaTest with embedded server enabled
Step-by-Step Solution
Solution:
  1. Step 1: Identify web environment options

    SpringBootTest supports different web environments: MOCK, RANDOM_PORT, DEFINED_PORT, NONE.
  2. Step 2: Understand RANDOM_PORT usage

    RANDOM_PORT starts the server on a random free port, enabling real HTTP calls via TestRestTemplate.
  3. Step 3: Exclude other options

    @WebMvcTest is for slice tests without starting the full server; MOCK environment does not start a real server.
  4. Final Answer:

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -> Option C
  5. Quick Check:

    Random port starts real server [OK]
Quick Trick: Use RANDOM_PORT for real HTTP integration tests [OK]
Common Mistakes:
  • Using MOCK environment expecting real HTTP calls
  • Confusing @WebMvcTest with full server startup
  • Assuming @DataJpaTest starts web server

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes