How to make a mock method return the argument passed to it in Mockito?
Hello everyone! I'm new to Java and Mockito, and I've encountered a problem while writing tests. I have a class that calls a repository method to save an object, and then uses the returned value (e.g., a generated ID). I want to mock this repository so that the method returns the same object that was passed, but with some modifications (e.g., an ID set). How can I do this?
Here's an example of the code I'm testing:
```java
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User createUser(String name, String email) {
User user = new User(name, email);
User savedUser = userRepository.save(user);
```
No answers yet.
Log in to leave an answer