0
0
JUnittesting~10 mins

Spring Boot @SpringBootTest in JUnit - 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 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'
ASpringBootTest
BAutowired
CComponent
DService
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
2fill in blank
medium

Complete 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'
AComponent
BService
CAutowired
DInject
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @Autowired for injection
Not annotating the field causes NullPointerException in tests
3fill in blank
hard

Fix 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'
AexpectedValue
B"expectedValue"
Cresult
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes instead of a string literal
Swapping expected and actual arguments in assertEquals
4fill in blank
hard

Fill 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'
AApplicationContext
BMyService
CApplicationContextRunner
DApplicationContextTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using the test class name as a type or constructor
Using a service class instead of context classes
5fill in blank
hard

Fill 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'
Acontext
B"myService"
CgetBeanName
DmyService
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