0
0
Spring Bootframework~10 mins

@SpringBootTest for integration tests - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate a test class for Spring Boot integration testing.

Spring Boot
@[1]
public class MyServiceIntegrationTest {
    // test methods here
}
Drag options to blanks, or click blank then click option'
AComponentScan
BConfiguration
CEnableAutoConfiguration
DSpringBootTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ComponentScan instead of @SpringBootTest
Forgetting to annotate the test class
Using @Configuration which is for config classes, not tests
2fill in blank
medium

Complete the code to inject a service bean into the test class.

Spring Boot
@SpringBootTest
public class MyServiceIntegrationTest {

    @[1]
    private MyService myService;

    // test methods
}
Drag options to blanks, or click blank then click option'
AInjectMocks
BAutowired
CResource
DMockBean
Attempts:
3 left
💡 Hint
Common Mistakes
Using @MockBean when you want the real bean
Using @InjectMocks which is from Mockito, not Spring
Forgetting to annotate the field
3fill in blank
hard

Fix the error in the test method to ensure the application context loads correctly.

Spring Boot
@SpringBootTest
public class MyServiceIntegrationTest {

    @Autowired
    private MyService myService;

    @Test
    public void contextLoads() {
        [1]
    }
}
Drag options to blanks, or click blank then click option'
Afail("Not implemented")
BassertTrue(false)
CassertNotNull(myService)
DSystem.out.println("Test")
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the test empty
Using fail() which always fails the test
Printing output instead of asserting
4fill in blank
hard

Fill both blanks to configure the test to run with a random port and inject the port number.

Spring Boot
@SpringBootTest(webEnvironment = [1])
public class WebIntegrationTest {

    @Value("${local.server.port}")
    private int [2];

    // test methods
}
Drag options to blanks, or click blank then click option'
ARANDOM_PORT
BDEFINED_PORT
Cport
DserverPort
Attempts:
3 left
💡 Hint
Common Mistakes
Using DEFINED_PORT which uses default port 8080
Injecting port into a field named 'port' but referencing differently
Not setting webEnvironment at all
5fill in blank
hard

Fill all three blanks to create a test that mocks a bean and verifies it was called.

Spring Boot
@SpringBootTest
public class ServiceMockTest {

    @MockBean
    private MyRepository [1];

    @Autowired
    private MyService [2];

    @Test
    public void testServiceCall() {
        [2].performAction();
        verify([1]).save(any());
    }
}
Drag options to blanks, or click blank then click option'
AmyRepository
BmyService
Crepository
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently
Not mocking the repository bean
Not verifying the mock interaction