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); } }
The test annotated with @SpringBootTest loads the full Spring application context. The assertion assertTrue(true) always passes, so the test will pass successfully.
Option C is incorrect because the assertion is true. Option C is incorrect because no configuration is missing. Option C is incorrect because @SpringBootTest with JUnit 5 does not require @RunWith.
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? } }
When a bean is loaded and injected by Spring, the field is not null. So assertNotNull(myService) correctly verifies the bean is present.
Options A, C, and D incorrectly assert the bean is null or not present.
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 } }
The test specifies a configuration class NonExistentConfig that does not exist. This causes Spring Boot to fail loading the application context, resulting in ApplicationContextException.
Option A is incorrect because the test method has @Test. Option A is incorrect because @SpringBootTest does not require additional properties. Option A is incorrect because extending a base class is not required.
@WebMvcTest loads only the web layer components such as controllers and related configurations, making tests faster and more focused.
Option D loads the full context and configures MockMvc. Option D is for JPA layer tests. Option D disables web environment but still loads full context.
Using webEnvironment = WebEnvironment.RANDOM_PORT starts the embedded server on a random free port. This allows tests to perform real HTTP calls without port conflicts. The port can be injected using @LocalServerPort.
Option B is incorrect because the server is started, not mocked. Option B is incorrect because the port is random, not fixed. Option B is incorrect because the Spring context is loaded.