How to Use JUnit with Spring Boot for Testing
To use
JUnit with Spring Boot, add the spring-boot-starter-test dependency which includes JUnit 5. Annotate your test classes with @SpringBootTest to load the application context and write test methods with @Test to verify your code.Syntax
Use @SpringBootTest on your test class to start the Spring Boot context. Use @Test to mark test methods. Inject beans with @Autowired to test Spring components.
@SpringBootTest: Loads full application context.@Test: Marks a method as a test.@Autowired: Injects Spring beans into tests.
java
import org.springframework.boot.test.context.SpringBootTest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @SpringBootTest public class MyServiceTest { @Autowired private MyService myService; @Test void testServiceMethod() { // test logic here } }
Example
This example shows a simple Spring Boot test class that checks if a service method returns the expected result.
java
import org.springframework.boot.test.context.SpringBootTest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest public class CalculatorServiceTest { @Autowired private CalculatorService calculatorService; @Test void testAdd() { int result = calculatorService.add(2, 3); assertEquals(5, result, "2 + 3 should equal 5"); } } // Service class import org.springframework.stereotype.Service; @Service public class CalculatorService { public int add(int a, int b) { return a + b; } }
Output
Test passed: testAdd()
Common Pitfalls
- Not including
spring-boot-starter-testdependency causes missing JUnit classes. - Forgetting
@SpringBootTestmeans Spring context won’t load, causingNullPointerExceptionon autowired beans. - Using JUnit 4 annotations (
@RunWith) instead of JUnit 5 (@ExtendWith) in new Spring Boot versions. - Writing tests without assertions leads to tests that always pass.
java
/* Wrong: Missing @SpringBootTest causes null bean */ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; public class WrongTest { @Autowired private CalculatorService calculatorService; // will be null @Test void testAdd() { // NullPointerException here int result = calculatorService.add(1, 1); } } /* Right: Add @SpringBootTest to load context */ import org.springframework.boot.test.context.SpringBootTest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest public class RightTest { @Autowired private CalculatorService calculatorService; @Test void testAdd() { int result = calculatorService.add(1, 1); assertEquals(2, result); } }
Quick Reference
Remember these key points when using JUnit with Spring Boot:
- Add
spring-boot-starter-testdependency. - Use
@SpringBootTestto load Spring context. - Write test methods with
@Testand include assertions. - Inject beans with
@Autowiredfor integration tests. - Use JUnit 5 annotations and features.
Key Takeaways
Add spring-boot-starter-test dependency to include JUnit and Spring test tools.
Annotate test classes with @SpringBootTest to load the full Spring Boot context.
Use @Test to mark test methods and include assertions to verify behavior.
Inject Spring beans in tests with @Autowired for integration testing.
Avoid mixing JUnit 4 and JUnit 5 annotations; prefer JUnit 5 in modern Spring Boot.