Following the precedent article “A convenient way to read resources in Java”, we will learn how to use @InjectResources to inject JUnit 5 and 4 tests with content of resources.

I am working a lot with Elasticsearch, so it’s not uncommon for me to deal with huge JSON objects. 90% of tests I write involves comparison of JSON documents.

In the precedent article we saw that reading resources with Java is harder than it should be. We also saw how to simplify this task using library @InjectResources. In this article we will see how to inject our JUnit 5/4 tests with content of resources.

Introduction

@InjectResources comes with extensions for popular test frameworks JUnit5 (https://hosuaby.github.io/inject-resources/0.1.0/asciidoc/#inject-resources-junit-jupiter) and JUnit4 (https://hosuaby.github.io/inject-resources/0.1.0/asciidoc/#inject-resources-junit-vintage).

Let’s start by adding necessary dependencies to our project:

With Gradle:

Or with Maven:

Let’s assume that we have a resource file /com/adelean/junit/jupiter/resource.txt containing this text:

The quick brown fox jumps over the lazy dog.

To inject this text on our JUnit5 tests we need:

  1. Add extension using annotation @TestWithResources
  2. Request the content of resource with @GivenTextResource

After that, the content of resource resource.txt appears inside the field resourceContent and can be used in tests.

JUnit4 on the other side uses Rules. The same operation with JUnit4 can be done as following:

It’s recommended to create resource rules using factory method com.adelean.inject.ressources.junit.vintage.GivenResource.givenResource imported statically. Following examples will omit that import.

Binary resources

To import binary resources we can use @GivenBinaryResource annotation for JUnit5 or BinaryResource rule for JUnit4.

With JUnit5:

With JUnit4:

Java properties

In a similar way, we can use annotation @GivenPropertiesResource or PropertiesResource rule to read Java properties.

With JUnit5:

With JUnit4:

We saw how the library handles simple formats. But it can do much more. We start the most interesting part of this article because @InjectResources considerably simplifies loading and parsing of JSON and YAML.

JSON and JSON lines

To parse JSON library natively supports Jackson and GSON. Let’s see how it works with Jackson:

In order for this snippet to work, jackson-core and jackson-databind must be present on the classpath. JSON parsing requires declaration of ObjectMapper annotated with @WithJacksonMapper. After, all we need is to add annotation @GivenJsonResource on the field of type Map, JsonNode or any POJO to get parsed content of JSON, or annotation @GivenJsonLinesResource for JSON lines.

Parsing with GSON works in similar manner. The official documentation gives us an example.

For Junit4 we have rules JsonResource and JsonLinesResource. The difference with JUnit5 annotations is that we need to supply ObjectMapper or Gson object to rules builder.

YAML

Parsing of YAML is similar to JSONs. For this purpose @InjectResources uses Snakeyaml that must be present on the classpath.

For JUnit5 use annotations @GivenYamlResource and @GivenYamlDocumentsResource (YAML documents is a format of a file that contains multiple YAML documents separated by three hyphens ---).

For JUnit4 use rules YamlResource and YamlDocumentsResource.

Conclusion

We finished this presentation of @InjectResources for JUnit. This tool can save you a lot of time if you work with resources a lot.

Do not hesitate to take a look into official documentation:

JUnit5 extension: https://hosuaby.github.io/inject-resources/0.1.0/asciidoc/#inject-resources-junit-jupiter

JUnit4 extension: https://hosuaby.github.io/inject-resources/0.1.0/asciidoc/#inject-resources-junit-vintage

GitHub repository: https://github.com/hosuaby/inject-resources

--

--