Get startedGet started for free

Additional annotations

1. Additional annotations

Let's review more ways to reduce verbosity!

2. Prerequisite: Object creation with .of()

Before we dive into the new syntax, let's look at a simpler way to create Java objects like lists. Instead of manually creating a list and adding elements, we can use "List of" and add elements there. This also works for Set and Map from the Java Collections library. Note that objects created with `.of()` are immutable - they cannot be changed.

3. Passing objects to @ParameterizedTest

Now let's discuss how to pass complex objects to a test. Take the `Person` class, with `firstName`, `lastName`, a constructor, and a `fullName()` method. What if we want to test `fullName` for multiple people?

4. Arguments class

We use the `Arguments` class for this. It's a container for any number objects of any kind. For example, we can create an `Arguments` object with a `Person` and a String, the expected full name. Let's now use it!

5. @MethodSource

Arguments objects are used as input for the `MethodSource` annotation. It is called `MethodSource` because it directs the test to a method that supplies the arguments. We specify the method name in the brackets of the `@MethodSource` annotation.

6. @MethodSource provider method

Finally, the method that provides arguments looks like this. It creates a list of Arguments using whatever methods we program. This method must be `static`. While other return types are allowed, we'll use a List of Arguments for simplicity.

7. @MethodSource provider method

Alternatively, we can use our new syntax for list creation and write the provider method like this:

8. What if we need more than arguments?

Here's one final trick. While not part of parameterized testing, it's useful for shared setup across tests. Consider the database test from earlier. We create two mocks and we pass them to the MessageProcessor constructor, then send a message and verify. When multiple tests share similar setup, we can simplify with the following trick.

9. @BeforeEach annotation

The BeforeEach annotation can be used to set up mocks and objects before each test in a class. It comes from the same package as @Test. To use it, we declare the necessary objects as fields in the test class:

10. Method for @BeforeEach

Next, annotate the method that initializes these fields with `@BeforeEach`. This will now execute before any test in the class, creating new mocks and a new MessageProcessor. Taking the object creation into a different method reduces the test to this:

11. Shortened test

This can greatly shorten test classes with lots of setup. Our test now contains only the action (when) and assertion (then) steps.

12. @BeforeEach full flow

Here's how it all comes together: First we declare shared fields for all tests. Then we write a beforeEach method to mock or initialize them, as a result, the instructions in BeforeEach execute before each test, so we can use them in every test

13. Let's practice!

Now, let's use your new knowledge by tiding and shortening tests!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.