Reading content of resource files in Java is harder that it should be. Great libraries like
Guava or Spring made it much
easier, but it's time to make another step forward. Use @InjectResources
is the easiest and most convenient way to load
and parse content of resources without boilerplate code that opens/closes streams and handles I/O exceptions. This library
made of inject-resources-core,
fluid Java DSL, and of extensions for
Spring,
JUnit5 and
JUnit4 that allow to do
that with simple annotations.
var text = resource()
.onClassLoaderOf(this.getClass())
.withPath("/com/adelean/junit/jupiter", "resource.txt")
.text();
Check Core user guide for more information and examples.
@Component
public class MyBean {
// Field 'text' is injected with content of '/com/adelean/junit/jupiter/resource.txt'
@TextResource("/com/adelean/junit/jupiter/resource.txt")
private String text;
}
Check Spring extension user guide for more information and examples.
@TestWithResources
class InjectTextResourcesTests {
@GivenTextResource("/com/adelean/junit/jupiter/resource.txt")
String instanceField;
@Test
public void testInjectTextIntoStringInstanceField() {
assertThat(instanceField)
.isEqualTo("The quick brown fox jumps over the lazy dog.");
}
}
Check JUnit5 extension user guide for more information and examples.
class MyTestClass {
@Rule
public ResourceRule<String> textResource = givenResource()
.text("/com/adelean/junit/jupiter/resource.txt")
.withCharset(StandardCharsets.UTF_8);
@Test
public void testWithTextResource() {
assertThat(textResource.get())
.isEqualTo("The quick brown fox jumps over the lazy dog.");
}
}
Check JUnit4 extension user guide for more information and examples.
- Binary
- Text
- Java properties
- JSON & JSON Lines
- YAML & YAML Documents
- 2022-04-07: Release
v0.3.2
. Upgradeorg.reflections
to0.10.2
. - 2022-03-23: Release
v0.3.1
. Limit classpath search forTestsAdvice
s. - 2021-09-04: Release
v0.3.0
. Fix source compatibility (Gradle). JUnit (5/4) modules now depends on core. Implementation no longer relies on classpath scan to find annotations of the project. - 2021-05-10: Release
v0.2.2
. Use reflexions.org instead of JUnit ReflectionSupport - 2021-04-23: Release
v0.2.1
. Fix reflexion on annotations on JDK 1.8 - 2021-03-07: Release
v0.2.0
. Publish to Maven Central. - 2020-08-13: Release
v0.1.0
. This release includes Spring extension. - 2020-07-29: Release
v0.1.0-beta
. This release contains support of YAML files (Snakeyaml) and JUnit 4 extension. - 2020-06-20: Release
v0.1.0-alpha
. The first ever release. Contains only modulecore
and JUnit 5 extension. Support files in formats: binary, text, java properties and JSON.