Introduction to Testing in Java
Maria Milusheva
Senior Software Engineer
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)
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;
}
}
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.
@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);
}
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
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"));
}
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); }
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;
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);
}
Test class then becomes:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
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