0
0
JUnittesting~20 mins

Spring Boot @SpringBootTest in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot @SpringBootTest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the test execution result of this Spring Boot test?
Consider this Spring Boot test class annotated with @SpringBootTest. What will be the test result when running this test?
JUnit
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest
public class SampleTest {

    @Test
    void contextLoads() {
        assertTrue(true);
    }
}
ATest fails with ApplicationContextException due to missing configuration.
BTest fails with AssertionError.
CTest passes successfully.
DTest is skipped because no @RunWith annotation is present.
Attempts:
2 left
💡 Hint
Remember that @SpringBootTest loads the full application context and the assertion is true.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a bean is loaded in @SpringBootTest?
Given a Spring Boot test annotated with @SpringBootTest, which assertion correctly checks that a bean of type MyService is loaded in the application context?
JUnit
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class BeanLoadTest {

    @Autowired
    private MyService myService;

    @Test
    void testBeanLoaded() {
        // Which assertion is correct here?
    }
}
AassertNotNull(myService);
BassertFalse(myService != null);
CassertTrue(myService == null);
DassertEquals(null, myService);
Attempts:
2 left
💡 Hint
If the bean is loaded, the injected field should not be null.
🔧 Debug
advanced
2:00remaining
Why does this @SpringBootTest test fail with ApplicationContextException?
This test fails with ApplicationContextException when run. What is the most likely cause?
JUnit
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = NonExistentConfig.class)
public class FailingTest {

    @Test
    void testSomething() {
        // test logic
    }
}
AThe specified configuration class NonExistentConfig does not exist, causing context load failure.
BThe test method is missing @Test annotation.
CThe @SpringBootTest annotation is missing required properties.
DThe test class must extend a Spring Boot base test class.
Attempts:
2 left
💡 Hint
Check the classes attribute in @SpringBootTest.
framework
advanced
2:00remaining
Which annotation combination is best to load only a slice of the Spring context for a web layer test?
You want to test only the web layer (controllers) without loading the full Spring Boot context. Which annotation combination achieves this?
A@SpringBootTest + @AutoConfigureMockMvc
B@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
C@DataJpaTest
D@WebMvcTest
Attempts:
2 left
💡 Hint
Look for the annotation designed for slicing web layer tests.
🧠 Conceptual
expert
2:00remaining
What is the effect of using @SpringBootTest with webEnvironment = WebEnvironment.RANDOM_PORT?
Select the correct statement about @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT):
AStarts the server on the default port 8080 regardless of the setting.
BStarts the embedded server on a random port and allows injecting the port number for real HTTP tests.
CDisables the embedded server and mocks all HTTP requests internally.
DRuns the test without loading the Spring context.
Attempts:
2 left
💡 Hint
Think about how to test real HTTP endpoints with a random port.