Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to annotate the test class for Spring Boot integration testing.
JUnit
@[1] public class MyServiceTest { // test methods }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @SpringBootTest on the test class
Forgetting the @SpringBootTest annotation causes tests to run without Spring context
✗ Incorrect
The @SpringBootTest annotation tells Spring Boot to look for a main configuration class and start the application context for integration testing.
2fill in blank
mediumComplete the code to inject a service bean into the test class.
JUnit
@SpringBootTest public class MyServiceTest { @[1] private MyService myService; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @Autowired for injection
Not annotating the field causes NullPointerException in tests
✗ Incorrect
The @Autowired annotation tells Spring to inject the MyService bean into the test class.
3fill in blank
hardFix the error in the test method to assert the service returns the expected value.
JUnit
@SpringBootTest public class MyServiceTest { @Autowired private MyService myService; @Test public void testGetValue() { String result = myService.getValue(); assertEquals([1], result); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes instead of a string literal
Swapping expected and actual arguments in assertEquals
✗ Incorrect
The expected value in assertEquals must be a string literal matching the expected output, so it needs quotes.
4fill in blank
hardFill both blanks to create a test that checks if the application context loads successfully.
JUnit
@SpringBootTest public class ApplicationContextTest { @Test public void contextLoads() { [1] context = new [2](); assertNotNull(context); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the test class name as a type or constructor
Using a service class instead of context classes
✗ Incorrect
ApplicationContextRunner is used to create and run an application context in tests, and ApplicationContext is the interface type.
5fill in blank
hardFill all three blanks to write a test that verifies a bean is present in the application context.
JUnit
@SpringBootTest public class BeanPresenceTest { @Autowired private ApplicationContext [1]; @Test public void testBeanExists() { boolean exists = [1].containsBean([2]); assertTrue(exists, "Bean should be present"); } private String [3]() { return "myService"; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable without quotes as bean name
Using inconsistent variable names for context
Not returning the correct bean name string
✗ Incorrect
The ApplicationContext is injected as 'context', the bean name is passed as a string literal, and the method getBeanName returns the bean name string.