How to properly mock void methods in Mockito?
Hi everyone! I'm a beginner Java developer writing unit tests using Mockito. I've encountered a problem: I can't figure out how to properly mock a method that returns nothing (void).
\n\nHere's a simple example. There's a class UserService that sends an email via EmailService:
public class EmailService {\n public void sendEmail(String to, String subject, String body) {\n // actual email sending\n System.out.println("Email sent to " + to);\n }\n}\n\npublic class UserService {\n private final EmailService emailService;\n \n public UserService(EmailService emailService) {\n this.emailService = emailService;\n }\n \n public void registerUser(String email) {\n // some registration logic No answers yet.
Log in to leave an answer
Blogs
Refactoring Legacy Code in Swift: A Step-by-Step Guide for iOS/macOS
Generics in PHP: Why They Are Needed and How to Use Them with Examples
Working with JSON in JavaScript: Parsing and Serialization for Beginners
How to read a file into a string in Java: under the hood analysis