junit-support
Contains interfaces and classes that make it easier to write unit tests with JUnit.
A quick overview of the functionality in this library:
Injecting resources
Instead of having to write a utility method to read the contents of a resource, simply annotate a field, constructor parameter or method parameter with @TestResource
, and JUnit will inject the resource for you:
@Test
void testWithResource(@TestResource("input.json") String json) {
// use json as needed
}
See @TestResource for more information, including options to configure how the resource is read or how to convert it to an object.
Reconfigure static loggers
Loggers are often defined as private static final
fields. That makes them difficult to mock. Using @TestLogger allows you to reconfigure these for test purposes.
Disable logging for tests
@DisableLogging allows you to suppress logging for tests.
Disable logging for successful tests
@LogOnFailure allows you to suppress logging for successful tests but not for failed tests.
Simplify writing JUnit extensions
If you want to write a JUnit extension that performs method lookups like @MethodSource, MethodLookup provides an easy to use API.
If you want to write a JUnit extension that can inject values into fields, constructor parameters or method parameters, some base classes are provided that let you focus on what's important - provide the values to inject.
Predefined tests
This library mainly contains several pre-defined tests, defined in interfaces that each test one small aspect of a class or interface, often a single method. This makes it easier to test custom implementations of various common interfaces or base classes. The currently supported list is:
- Collection, both modifiable and unmodifiable
- Iterable
- Iterator, both modifiable and unmodifiable
- List and ListIterator, both modifiable and unmodifiable
- Map and Map.Entry, both modifiable and unmodifiable
- Set, both modifiable and unmodifiable
- Spliterator
- InputStream, OutputStream, Reader and Writer
In addition, there are pre-defined tests for MethodDelegation and covariant return types.
Additional assertions
Several additional assertions are provided. Some examples:
- alternatives to
assertTrue
andassertFalse
that provide better failure messages - assertions for checking the content of a
Reader
orInputStream
- assertions for
Optional
,OptionalInt
,OptionalLong
andOptionalDouble
- assertions for exception causes
- assertions for code that can can throw more than one different types of exceptions
- assertions for code that optionally throws an exception
Parameterized test support
JUnit Pioneer has @CartesianTest to provide the Cartesian product of sets of arguments. Using @CartesianTest.MethodFactory
allows you to create argument sets programmatically. It does not provide the possibility to filter out combinations though. Class ArgumentsCombiner works like JUnit Pioneer's ArgumentSets
class but allows filtering out combinations.
Testing concurrent code
Some classes for testing concurrent code are provided.