From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Verify arguments with argument captors

Verify arguments with argument captors

- [Instructor] Argument captors provide an alternative approach for verifying your methods are called with the correct arguments. Instead of merely specifying conditions that the arguments should match, argument captors allow you to capture and inspect the actual arguments passed. This enables more detailed inspections of the arguments after the method call, making your tests more robust. Let's take a look at an example. Here we have a test that verifies some processing for a transaction. In our verification, we use an argument matcher to verify the third argument is a timestamp. Let's modify this to use an argument captor so we can see the actual value of the timestamp passed. To start off, we'll create an argument captor for our timestamp. This is done as part of the method, but we can also make this a field if we want to use the captor in multiple tests. If we make it a field, we can leverage the captor annotation. The captor annotation creates an argument captor instance that can…

Contents