Complete the code to annotate a test class for Spring Boot integration testing.
@[1] public class MyServiceIntegrationTest { // test methods here }
The @SpringBootTest annotation tells Spring Boot to look for a main configuration class and start the full application context for integration tests.
Complete the code to inject a service bean into the test class.
@SpringBootTest public class MyServiceIntegrationTest { @[1] private MyService myService; // test methods }
The @Autowired annotation tells Spring to inject the bean from the application context into the test class.
Fix the error in the test method to ensure the application context loads correctly.
@SpringBootTest public class MyServiceIntegrationTest { @Autowired private MyService myService; @Test public void contextLoads() { [1] } }
The contextLoads test should verify that the service bean is injected and not null, confirming the context started properly.
Fill both blanks to configure the test to run with a random port and inject the port number.
@SpringBootTest(webEnvironment = [1]) public class WebIntegrationTest { @Value("${local.server.port}") private int [2]; // test methods }
Using webEnvironment = RANDOM_PORT starts the server on a random port. The port is injected into the serverPort field using @Value.
Fill all three blanks to create a test that mocks a bean and verifies it was called.
@SpringBootTest public class ServiceMockTest { @MockBean private MyRepository [1]; @Autowired private MyService [2]; @Test public void testServiceCall() { [2].performAction(); verify([1]).save(any()); } }
The @MockBean creates a mock of MyRepository named myRepository. The service myService is autowired. The test calls myService.performAction() and verifies myRepository.save() was called.