Bird
0
0

Given this test class snippet:

medium📝 component behavior Q4 of 15
Spring Boot - Testing Spring Boot Applications
Given this test class snippet:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MyTest {
  @Autowired
  private TestRestTemplate restTemplate;

  @Test
  void testHello() {
    String body = restTemplate.getForObject("/hello", String.class);
    System.out.println(body);
  }
}

What will be printed if the /hello endpoint returns "Hello World"?
AHello World
Bnull
CError: NoSuchBeanDefinitionException
DEmpty string
Step-by-Step Solution
Solution:
  1. Step 1: Understand the use of RANDOM_PORT and TestRestTemplate

    With RANDOM_PORT, the embedded server starts on a random port and TestRestTemplate is auto-configured to use it.
  2. Step 2: Analyze the test method behavior

    The restTemplate calls /hello endpoint which returns "Hello World", so the printed output is "Hello World".
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    TestRestTemplate calls endpoint = Hello World [OK]
Quick Trick: TestRestTemplate works with RANDOM_PORT to call real endpoints [OK]
Common Mistakes:
  • Expecting null because of missing port config
  • Confusing TestRestTemplate with mocked beans
  • Assuming empty string if endpoint not found

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes