Bird
Raised Fist0
Spring Bootframework~20 mins

@SpringBootTest for integration tests - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SpringBootTest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does @SpringBootTest do in a test class?

Consider a test class annotated with @SpringBootTest. What is the main effect of this annotation when running the test?

AIt loads the full Spring application context for integration testing.
BIt only loads the web layer components without starting the full context.
CIt mocks all Spring beans automatically to isolate the test.
DIt disables Spring context loading and runs tests as plain unit tests.
Attempts:
2 left
💡 Hint

Think about whether the whole app context is started or just parts of it.

📝 Syntax
intermediate
2:00remaining
Which @SpringBootTest property controls the web environment?

Look at these options for @SpringBootTest(webEnvironment = ...). Which one starts the embedded server on a random port?

AWebEnvironment.DEFINED_PORT
BWebEnvironment.MOCK
CWebEnvironment.NONE
DWebEnvironment.RANDOM_PORT
Attempts:
2 left
💡 Hint

Random port means the server starts on any free port to avoid conflicts.

🔧 Debug
advanced
2:00remaining
Why does this @SpringBootTest fail to inject a bean?

Given this test class:

  @SpringBootTest
  public class MyServiceTest {
    @Autowired
    private MyService myService;

    @Test
    void testService() {
      assertNotNull(myService);
    }
  }

The test fails because myService is null. What is the likely cause?

AThe <code>@SpringBootTest</code> annotation is missing the <code>classes</code> attribute specifying the config.
BThe <code>MyService</code> bean is not annotated with <code>@Service</code> or <code>@Component</code>.
CThe <code>myService</code> field should be static for injection to work.
DThe test class is missing <code>@RunWith(SpringRunner.class)</code> annotation.
Attempts:
2 left
💡 Hint

Check if the bean is properly registered in the Spring context.

❓ state_output
advanced
2:00remaining
What is the output of this @SpringBootTest with properties override?

Consider this test class:

@SpringBootTest(properties = {"app.feature.enabled=false"})
public class FeatureTest {

  @Value("${app.feature.enabled}")
  private boolean featureEnabled;

  @Test
  void testFeatureFlag() {
    System.out.println(featureEnabled);
  }
}

What will be printed when the test runs?

Afalse
Btrue
Cnull
DThrows an exception due to missing property
Attempts:
2 left
💡 Hint

Properties in @SpringBootTest override application properties.

🧠 Conceptual
expert
3:00remaining
Why prefer @SpringBootTest over @WebMvcTest for full integration tests?

Which reason best explains why @SpringBootTest is preferred over @WebMvcTest for full integration testing?

A@WebMvcTest disables Spring Security, so it cannot test secured endpoints.
B@SpringBootTest runs tests faster because it loads fewer beans than @WebMvcTest.
C@SpringBootTest loads the entire application context including services and repositories, enabling end-to-end testing.
D@WebMvcTest automatically mocks all dependencies, so it cannot test real database interactions.
Attempts:
2 left
💡 Hint

Think about how much of the app is loaded and tested.

Practice

(1/5)
1. What is the main purpose of using @SpringBootTest in a Spring Boot project?
easy
A. To configure database connections only
B. To mock all dependencies automatically
C. To run unit tests without loading Spring context
D. To load the full application context for integration testing

Solution

  1. Step 1: Understand what @SpringBootTest does

    This annotation loads the entire Spring Boot application context, allowing tests to run with all beans and configurations loaded.
  2. Step 2: Compare with other testing annotations

    Unlike unit tests or mocks, @SpringBootTest is used for integration tests that need the full app context.
  3. Final Answer:

    To load the full application context for integration testing -> Option D
  4. Quick Check:

    @SpringBootTest = full app context load [OK]
Hint: Remember: @SpringBootTest loads full app context for integration [OK]
Common Mistakes:
  • Thinking it mocks all dependencies automatically
  • Confusing it with unit test annotations
  • Assuming it only configures database connections
2. Which of the following is the correct way to use @SpringBootTest to start the web environment on a random port?
easy
A. @SpringBootTest(webEnv = RANDOM_PORT)
B. @SpringBootTest(webEnvironment = "random")
C. @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
D. @SpringBootTest(port = RANDOM_PORT)

Solution

  1. Step 1: Recall the correct attribute name and value

    The attribute to set the web environment is webEnvironment, and the enum value for random port is WebEnvironment.RANDOM_PORT.
  2. Step 2: Check syntax correctness

    @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) uses the correct attribute and enum syntax. Other options use incorrect attribute names or string values.
  3. Final Answer:

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

    Correct attribute and enum = @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) [OK]
Hint: Use webEnvironment = WebEnvironment.RANDOM_PORT exactly [OK]
Common Mistakes:
  • Using wrong attribute name like webEnv
  • Passing string instead of enum
  • Trying to set port directly in annotation
3. 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!".
medium
A. Hello, Spring!
B. null
C. 404 Not Found error
D. Test fails with context load error

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]
Hint: 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
4. You wrote this test but it fails with a NoSuchBeanDefinitionException for TestRestTemplate. What is the likely cause?
\@SpringBootTest
class MyTest {
  \@Autowired
  private TestRestTemplate restTemplate;

  \@Test
  void test() {
    // test code
  }
}
medium
A. TestRestTemplate is not a Spring bean
B. Missing webEnvironment setting to start web server
C. Forgot to add @Autowired annotation
D. Test class must be public

Solution

  1. Step 1: Identify why TestRestTemplate bean is missing

    By default, @SpringBootTest does not start the web server unless webEnvironment is set, so TestRestTemplate bean is not created.
  2. Step 2: Fix by adding webEnvironment

    Adding webEnvironment = WebEnvironment.RANDOM_PORT or DEFINED_PORT starts the server and creates the bean.
  3. Final Answer:

    Missing webEnvironment setting to start web server -> Option B
  4. Quick Check:

    webEnvironment needed for TestRestTemplate bean [OK]
Hint: Add webEnvironment to enable TestRestTemplate bean [OK]
Common Mistakes:
  • Assuming TestRestTemplate is always available
  • Forgetting to set webEnvironment in @SpringBootTest
  • Thinking @Autowired is missing
5. You want to write an integration test that loads the full Spring Boot context but disables the web server startup to speed up tests. Which @SpringBootTest configuration should you use?
hard
A. @SpringBootTest(webEnvironment = WebEnvironment.NONE)
B. @SpringBootTest(webEnvironment = WebEnvironment.MOCK)
C. @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
D. @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)

Solution

  1. Step 1: Understand webEnvironment options

    WebEnvironment.NONE disables web server startup but loads full context. MOCK uses mock servlet environment, RANDOM_PORT and DEFINED_PORT start real servers.
  2. Step 2: Choose option that disables web server

    To speed up tests without web server, use WebEnvironment.NONE.
  3. Final Answer:

    @SpringBootTest(webEnvironment = WebEnvironment.NONE) -> Option A
  4. Quick Check:

    Disable web server with WebEnvironment.NONE [OK]
Hint: Use WebEnvironment.NONE to skip web server startup [OK]
Common Mistakes:
  • Using MOCK which still sets up servlet environment
  • Starting real server with RANDOM_PORT or DEFINED_PORT
  • Confusing NONE with MOCK
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard