Unit Testing with JUnit & Mockito
Unit testing with JUnit 5, Mockito for mocks, assertions, @Mock, @InjectMocks, test doubles
1What is JUnit 5 Jupiter?
What is JUnit 5 Jupiter?
Answer
JUnit 5 Jupiter is the modern testing engine that replaces JUnit 4, offering an improved API with modernized annotations and new features. Jupiter brings richer assertions, better support for parameterized tests, and a modular architecture allowing easy framework extension. Using Jupiter enables leveraging Java 8+ features like lambdas in assertions.
2Which annotation marks a method as a test in JUnit 5?
Which annotation marks a method as a test in JUnit 5?
Answer
The @Test annotation from JUnit 5 identifies a method as a test case that will be executed by the runner. Unlike JUnit 4 where @Test accepted parameters like expected or timeout, JUnit 5 uses dedicated assertions like assertThrows. Each @Test method should be autonomous and test a specific behavior in isolation.
3What is the difference between @BeforeEach and @BeforeAll?
What is the difference between @BeforeEach and @BeforeAll?
Answer
@BeforeEach runs before each test method to initialize a clean state, while @BeforeAll runs once before all tests in the class. @BeforeEach is ideal for creating fresh mocks or objects for each test, ensuring isolation. @BeforeAll requires a static method and is used for expensive initializations shared across all tests.
Which assertion verifies equality between two values?
How to verify that an exception is thrown in JUnit 5?
+27 interview questions
Other Spring Boot interview topics
Spring Core - IoC & DI
Spring Boot Auto-Configuration
Spring Boot Starters
Application Properties & YAML
Logging with SLF4J & Logback
Spring Boot DevTools
Spring MVC Basics
Spring REST Controllers
Request & Response Handling
Exception Handling
Bean Validation
Spring Data JPA Basics
JPA Entities & Relationships
JPA Queries
Spring Data Repositories
Spring Security Basics
Spring Boot Actuator
Spring Boot Testing
Profiles & Environment
RestTemplate & WebClient
Async & Scheduling
Caching with Spring
Spring WebFlux (Reactive)
Spring Transactions
Advanced Authentication & Authorization
JWT & Stateless Security
OAuth2 & Authorization Server
Spring Boot & Docker
Microservices with Spring
Spring Cloud Config
Performance Optimization
GraalVM Native Images
Master Spring Boot for your next interview
Access all questions, flashcards, technical tests, code review exercises and interview simulators.
Start for free