Bird
0
0

Given the following test class snippet, what will be the output when running the test?

medium📝 component behavior Q13 of 15
Spring Boot - Testing Spring Boot Applications
Given the following test class snippet, what will be the output when running the test?
\@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class MyIntegrationTest {
  \@Autowired
  private TestRestTemplate restTemplate;

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

Assuming the /hello endpoint returns "Hello, Spring!".
AHello, Spring!
Bnull
C404 Not Found error
DTest fails with context load error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the test setup

    The test uses @SpringBootTest with RANDOM_PORT and injects TestRestTemplate, which allows calling the real HTTP endpoint.
  2. Step 2: Predict the endpoint call result

    The /hello endpoint returns "Hello, Spring!" so the call will return this string and print it.
  3. Final Answer:

    Hello, Spring! -> Option A
  4. Quick Check:

    TestRestTemplate calls real endpoint = "Hello, Spring!" [OK]
Quick Trick: TestRestTemplate calls real endpoint in @SpringBootTest [OK]
Common Mistakes:
  • Expecting null because of missing mocks
  • Assuming 404 because of wrong port
  • Thinking context fails to load

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes