-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.in
78 lines (58 loc) · 1.99 KB
/
README.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
## zelador
A minimalist JUnit 5 extension to register `AutoCloseable` resources that
will be destroyed after tests finish executing.
### Features
* Written in pure Java 21.
* [OSGi](https://www.osgi.org/) ready.
* [JPMS](https://en.wikipedia.org/wiki/Java_Platform_Module_System) ready.
* ISC license.
* High-coverage automated test suite.
### Motivation
For whatever reason, [JUnit 5](https://junit.org/junit5/) does not include
any utility to register per-test resources and automatically have them
cleaned up.
The `zelador` package provides a tiny [JUnit 5](https://junit.org/junit5/)
extension that allows for registering `AutoCloseable` objects that must be
closed after tests finish executing.
### Building
```
$ mvn clean verify
```
### Usage
Annotate your test suite with `@ExtendWith(ZeladorExtension.class)`. This
will allow tests to get access to an injected `CloseableResourcesType`
instance that can be used to register resources to be closed.
```
@ExtendWith(ZeladorExtension.class)
public final class ExampleTest
{
@Test
public void test0(
final CloseableResourcesType resources)
{
var output = resources.addPerTestResource(Files.newOutputStream(...));
...
}
@Test
public void test1(
final CloseableResourcesType resources)
{
var output = resources.addPerTestClassResource(Files.newOutputStream(...));
...
}
}
```
Resources added with `addPerTestResource` will be closed after the individual
test has finished (as if it had been closed in an `@AfterEach` method).
Resources added with `addPerTestClassResource` will be closed after all tests
in the class have finished (as if it had been closed in an `@AfterAll` method).
`AutoCloseable` is a functional interface and, as such, it's trivial to pass
objects to the `CloseableResourcesType` class that do not implement
`AutoCloseable` by simply using a method reference:
```
class NotReallyCloseable {
public void finish() { ... }
}
NotReallyCloseable x;
resources.addPerTestResource(x::finish);
```