Additional annotations

Introduction to Testing in Java

Maria Milusheva

Senior Software Engineer

Prerequisite: Object creation with .of()

Consider:

List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);

Quicker and shorter way instead:

List<Integer> newList = List.of(10, 20, 30);
  • Works the same for Set and Map from Java Collections

  • Objects created with .of() are typically immutable (can't be changed)

Introduction to Testing in Java

Passing objects to @ParameterizedTest

Consider the following class:

class Person {
    String firstName;
    String lastName;
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String fullName(Person person) {
        return firstName + " " + lastName;
    }
}
Introduction to Testing in Java

Arguments class

We use Arguments to pass multiple args of any type.

Arguments - can contain any number of objects of any kind.

For example:

Arguments.of(new Person("Monty", "Python"), "Monty Python");

We can now use it in the next @ParameterizedTest type.

Introduction to Testing in Java

@MethodSource

@MethodSource allows us to pass any object to a test.

The test looks like:

@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
    assertEquals(person.fullName(person), expectedFullName);
}
Introduction to Testing in Java

@MethodSource provider method

Method for @MethodSource:

private static List<Arguments> provideNames() {
    List<Arguments> args = new ArrayList<>();
    args.add(Arguments.of(new Person("Robert", "Martin"), "Robert Martin"));
    args.add(Arguments.of(new Person("Heinz", "Kabutz"), "Heinz Kabutz"));

    return args;
}

Note: method needs to be static

Note: many permitted return types; List<Arguments> is the simplest

1 https://junit.org/junit5/docs/5.2.0/api/org/junit/jupiter/params/provider/MethodSource.html
Introduction to Testing in Java

@MethodSource provider method

Method for @MethodSource:

private static List<Arguments> provideNames() {
    return List.of(
        Arguments.of(new Person("John", "Doe"), "John Doe"),
        Arguments.of(new Person("Jane", "Doe"), "Jane Doe"),
        Arguments.of(new Person("Alice", "Bob"), "Alice Bob"));
}
Introduction to Testing in Java

What if we need more than arguments?

Consider the databases test:

@Test
void process_savesToInfoStore_whenInfoMessage() {
  InfoStore infoStore = mock(InfoStore.class);
  ErrorStore errorStore = mock(ErrorStore.class);
  MessageProcessor messageProcessor = new MessageProcessor(infoStore, errorStore);

messageProcessor.saveMessage("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started."); verifyNoInteractions(errorStore); }
Introduction to Testing in Java

@BeforeEach annotation

We can use @BeforeEach annotation to create a method that executes before each test:

import org.junit.jupiter.api.BeforeEach;

To use it, first declare objects as fields:

class MessageProcessorTest {

    private InfoStore infoStore;
    private ErrorStore errorStore;
    private MessageProcessor messageProcessor;
Introduction to Testing in Java

Method for @BeforeEach

Create each object in a separate method:

@BeforeEach
void setUp() {
    this.infoStore = mock(InfoStore.class);
    this.errorStore = mock(errorStore.class);
    this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Introduction to Testing in Java

Shortened test

Test class then becomes:

@Test
void process_savesToInfoStore_whenInfoMessage() {
    messageProcessor.process("[INFO] Process started.");

    verify(infoStore).save("[INFO] Process started."); 
    verifyNoInteractions(errorStore); 
}
Introduction to Testing in Java

@BeforeEach full flow

All together:

class MessageProcessorTest {
    private InfoStore infoStore; // Declare fields


@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Initialize fields }
@Test void process_savesToInfoStore_whenInfoMessage() { // Use fields } }
Introduction to Testing in Java

Let's practice!

Introduction to Testing in Java

Preparing Video For Download...