Below you will find pages that utilize the taxonomy term “Test”
Annotation Processor Testing
Creating an Annotation Processor means that you will likely also want to add tests, but this can be tricky since the runtime is the compilation process itself, meaning that you will have to invoke a compilation step during test execution. In this post I’d like to outline what you can do to ensure proper test coverage.
Annotation Processing
In Java, Annotations allow compile-time metaprogramming, meaning that it allows creating code based on other source code. I use this in Java - Lenses to create auxiliary classes for annotated records. These contain various instances and functions which need the type information of the annotated records, meaning that there is no way to this directly in the source code itself.
Json Unit Testing
Many applications have APIs consuming and returning JSON. If you do not cover the expected inputs and output of the API you risk introducing unintentional changes. Luckily it is not hard to create these valuable test cases. If you have types that are used both as symmetrical inputs and outputs this test pattern will provide full coverage and give you utility methods for other tests as well.
For this example I’ll be using a type Task
having just a name
and a description
field, both String
s:
Awaitility
Writing tests on asynchronous code can be a challenge. Given an asynchronous process to test, we may try to use custom code to wait for the process to finish or reach a certain state. This may cause us to end up with tests that are flaky, slow, or hard to understand. It is not uncommon for legacy projects to have these setups, and perhaps they can be improved a little.
Java - Unique Enum Field values
When you have a Java Enum with a field, a requirement can be that the field value needs to be unique. Using a utility method it can be easy to create a test for this. Given an Enum class with a field (using some Lombok):
@Getter
@RequiredArgsConstructor
@Accessors(fluent = true)
public enum Sides {
LEFT("left"),
RIGHT("right");
final String label;
}
You can enforce uniqueness using a unit test:
import io.vavr.collection.HashMap;
import io.vavr.collection.Vector;
import org.junit.jupiter.api.Test;
import java.util.function.Function;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SidesTest {
@Test
void unique() {
assertUnique(Sides.values(), Sides::label);
}
private static <T> void assertUnique(T[] values, Function<T, String> fieldValue) {
assertEquals(
HashMap.empty(),
Vector.of(values)
.groupBy(fieldValue)
.filter(it -> it._2().size() > 1));
}
}
The test will automatically validate that all fields have distinct values, even if the Enum expands or changes in the future. Failures will report the enum values that have the same property. If we misconfigure the enum above ( RIGHT("left")
) we see which value occurred multiple times, and in which Enum values: