カスタムAnswerとコールバック
`Answer`インターフェースを使ってモックメソッド呼び出しのカスタムロジックを実装し、複雑な振る舞いを再現します。
「カスタムAnswerとコールバック」はCoddyKit上の無料Testing Mastery: JUnit, Mockito & Integration Testsレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはTesting Mastery: JUnit, Mockito & Integration Tests学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond Simple Stubbing
Sometimes, simply returning a fixed value with when().thenReturn() isn't enough for your mock objects.
- What if a mock method needs to modify an argument passed to it?
- What if it needs to execute a callback function?
- What if its return value depends on multiple inputs in a complex, dynamic way?
These advanced scenarios call for custom logic within your mocks.
Introducing Mockito's Answer
Mockito provides the org.mockito.stubbing.Answer interface. This powerful tool lets you define custom behavior for a mocked method call.
- Think of it as writing a small piece of code that runs whenever the mocked method is invoked.
- The
Answerinterface has a single method:Object answer(Invocation invocation) throws Throwable;.
It gives you fine-grained control over mock responses.
Anatomy of answer()
The answer() method is where you implement your custom logic. It receives an InvocationOnMock object (often just referred to as invocation).
- The
InvocationOnMockobject provides crucial details about the method call:- Which method was called?
- What arguments were passed?
- Which mock object was involved?
You can use these details to decide what to return, throw an exception, or perform side effects.
Using doAnswer for Flexibility
While you can use when().thenAnswer(), Mockito's doAnswer() method is often more flexible and widely used, especially for void methods or when combining with other stubbings.
doAnswer()allows you to specify behavior for a method call before definingwhen().- It's particularly useful when you need to:
- Capture arguments.
- Invoke callbacks.
- Change the state of objects passed as arguments.
Custom Return Logic
Let's see how to use doAnswer to return a value that depends on the input argument. Here, our mock will double the input number.
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
// A simple interface to mock
interface Calculator {
int calculate(int input);
}
public class Main {
public static void main(String[] args) {
// Create a mock object for our Calculator interface
Calculator mockCalculator = Mockito.mock(Calculator.class);
// Define custom answer to double the input argument
Mockito.doAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
// Get the first argument (index 0) as an Integer
Integer arg = invocation.getArgument(0);
return arg * 2; // Return double the argument
}
}).when(mockCalculator).calculate(Mockito.anyInt()); // Apply to any int input
// Test the custom behavior of the mock
System.out.println("Result for 5: " + mockCalculator.calculate(5));
System.out.println("Result for 10: " + mockCalculator.calculate(10));
}
}Simulating Callbacks
A very common and powerful use case for doAnswer is simulating asynchronous operations or invoking callbacks.
- Imagine a service that takes a
Callbackinterface (e.g., aConsumer) as an argument. - You can use
doAnswerto manually invoke theonSuccessoronFailuremethod of that callback.
This allows you to test how your application code reacts to different callback outcomes without actual asynchronous execution.
Mocking a Callback
Here, we simulate a DataService calling a Consumer callback. doAnswer lets us trigger the callback with a specific value when fetchData is called.
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.function.Consumer;
// An interface representing a service that fetches data asynchronously
interface DataService {
void fetchData(String query, Consumer<String> callback);
}
public class Main {
public static void main(String[] args) {
DataService mockService = Mockito.mock(DataService.class);
// Configure the mock to simulate fetching data and calling the callback
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
// Get the arguments passed to fetchData
String query = invocation.getArgument(0); // The first argument is the query
Consumer<String> callback = invocation.getArgument(1); // The second is the callback
// Simulate a successful data fetch by invoking the callback
callback.accept("Data for '" + query + "' fetched successfully!");
return null; // Void methods in doAnswer return null
}
}).when(mockService).fetchData(Mockito.anyString(), Mockito.any(Consumer.class));
// Our application code would typically use this service
System.out.println("Application requesting user data...");
mockService.fetchData("user:456", result -> {
System.out.println("Application received data: " + result);
});
System.out.println("Application finished request setup.");
}
}Deeper with Invocation
The InvocationOnMock object (or simply invocation) passed to your answer() method is a powerful context object.
invocation.getArguments(): Returns an array of all arguments passed to the mocked method.invocation.getArgument(index): Returns a specific argument by its index.invocation.getMethod(): Gives you theMethodobject that was called, allowing reflection.invocation.getMock(): Returns the mock object itself.invocation.callRealMethod(): For spies, this allows invoking the actual, real method.
Tips for Answer Use
Custom answers are powerful, but it's important to use them thoughtfully to keep your tests maintainable and clear:
- Keep them simple: If your
answerlogic becomes too complex, it might indicate a design issue in your production code or an overly complicated test setup. - Prefer simpler stubbing: Always choose simpler stubbing methods like
thenReturn(),thenThrow(), orthenCallRealMethod()if they can achieve the desired behavior. - Test the
Answeritself: For very complexAnswerimplementations, consider extracting them into a separate class and writing a unit test for that class.
When to Use Answer
Here are typical situations where the Answer interface (usually via doAnswer()) truly shines:
- Dynamic return values: When the return value depends on input arguments in a non-trivial or calculated way.
- Side effects: Modifying arguments that are passed by reference (e.g., updating an object).
- Callbacks: Triggering success or failure callbacks to simulate asynchronous operations.
- Chaining operations: Simulating a sequence of interactions or state changes within a single mock call.
Custom Mock Behavior
You are testing a UserService that depends on a UserRepository. The UserRepository has a method save(User user) which typically updates the user's ID after saving it to a database.
You want to mock UserRepository to simulate this behavior without hitting a real database. Assume a User class with setId(Long id).
Recap: Dynamic Mocks
You've learned how to bring dynamic and complex behavior to your mocks using Mockito's Answer interface and the doAnswer method.
- The
Answerinterface lets you define custom logic that executes when a mocked method is called. - The
InvocationOnMockobject provides essential context, including method arguments and the mock itself. doAnsweris ideal for dynamic return values, simulating callbacks, or modifying arguments passed by reference.
Remember to use `Answer` judiciously, preferring simpler stubbing when possible, to keep your tests clear and focused.
よくある質問
「カスタムAnswerとコールバック」レッスンは無料ですか?
はい。「カスタムAnswerとコールバック」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Testing Mastery: JUnit, Mockito & Integration Testsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。
「カスタムAnswerとコールバック」で何を学びますか?
`Answer`インターフェースを使ってモックメソッド呼び出しのカスタムロジックを実装し、複雑な振る舞いを再現します。 ブラウザで直接実行するハンズオンコードでTesting Mastery: JUnit, Mockito & Integration Testsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Testing Mastery: JUnit, Mockito & Integration Testsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのTesting Mastery: JUnit, Mockito & Integration Testsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「カスタムAnswerとコールバック」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このTesting Mastery: JUnit, Mockito & Integration Testsレッスンでコードを書いて実行できますか?
はい。すべてのTesting Mastery: JUnit, Mockito & Integration Testsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- カスタムAnswerとコールバック
- 静的メソッドとコンストラクターのモック化
- Mockitoのベストプラクティス
- ArgumentCaptorによる引数の捕捉