How do you create a mock object in Mockito?
Create mocks in Mockito with mock() or @Mock, stub behaviour with when().thenReturn() and check calls with verify, using JUnit 5 and Mockito 5.
2,887 posts about Java: questions with answers, tutorials and articles, newest first.
Create mocks in Mockito with mock() or @Mock, stub behaviour with when().thenReturn() and check calls with verify, using JUnit 5 and Mockito 5.
Create JUnit 5 suites with @Suite and @SelectClasses or @SelectPackages, filter by tags, and avoid duplicate runs in Maven.
@BeforeEach marks a JUnit 5 method that runs before every test so each one starts from a clean, known state. It replaces @Before from JUnit 4.
Use assertThrows in JUnit 5 to verify the exception type, then assert on its message, cause or custom fields. Includes migration notes from JUnit 4.
Assertions compare expected and actual results in a JUnit test and fail it when they differ. This guide covers the main methods with runnable examples.
Run one JUnit test with many inputs using @ParameterizedTest and the value, CSV, method and enum sources, with examples and tips.
@Test tells JUnit to run a method as a test. See lifecycle, assertThrows, timeouts, parameterized tests and the differences from JUnit 4.
Write JUnit 5 unit tests with @Test, assertions, lifecycle annotations, parameterized tests and Mockito, with Maven and Gradle setup for current Java projects.
JUnit 5 and TestNG compared side by side with the same test in both, so you can pick the right Java testing framework for your project.
Keep settings outside your code in Spring using property files, @Value, Environment, profiles and typed @ConfigurationProperties classes.
@ConfigurationProperties binds application properties to typed Java objects with validation. Learn records, registration and how it differs from @Value.
Use ${key} and ${key:default} with @Value to inject values from properties files, environment variables and command-line arguments into Spring beans.
The @Value annotation injects property values, defaults and SpEL results into Spring beans. See placeholders, SpEL, limits and when to use @ConfigurationProperties.
Set spring.profiles.active in properties, on the command line or in Docker, then use profile files, @Profile and @ActiveProfiles correctly.
Spring Profiles let one build run in dev, test and prod by activating beans and configuration files for the chosen environment.
Use Spring profiles, profile-specific property files and @Profile beans to run the same build in dev, staging and production with the right settings.
@Primary marks the default bean Spring injects when several beans share a type. See how it is resolved, how @Qualifier overrides it, and what to avoid.
Learn how to write a custom BeanPostProcessor in Spring, where it runs in the bean lifecycle, how to register and order it, and when AOP is a better fit.
Component scanning lets Spring find @Component classes and register them as beans. See how to configure it and fix missing beans.
@ComponentScan makes Spring discover annotated classes in chosen packages and register them as beans. Covers Spring Boot defaults, filters and common errors.