Bird
0
0

Which annotation combination is correct to enable TestRestTemplate with a random port in a Spring Boot test?

easy📝 Syntax Q12 of 15
Spring Boot - Testing Spring Boot Applications
Which annotation combination is correct to enable TestRestTemplate with a random port in a Spring Boot test?
A@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
B@SpringBootTest(webEnvironment = WebEnvironment.NONE)
C@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
D@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
Step-by-Step Solution
Solution:
  1. Step 1: Identify webEnvironment for real HTTP tests

    Using RANDOM_PORT starts the server on a random port for real HTTP calls.
  2. Step 2: Understand other options

    NONE disables web environment, MOCK uses mock servlet environment, DEFINED_PORT uses fixed port.
  3. Final Answer:

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

    RANDOM_PORT enables real HTTP server [OK]
Quick Trick: Use RANDOM_PORT for real HTTP server in tests [OK]
Common Mistakes:
  • Using MOCK disables real HTTP server
  • Choosing NONE disables web environment
  • Assuming DEFINED_PORT is random

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes