Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oswaldobapvicjr/junit-utils
Common utilities for working with JUnit
https://github.com/oswaldobapvicjr/junit-utils
assertion instantiation java junit junit-utils testing testing-tool unit-testing
Last synced: about 1 month ago
JSON representation
Common utilities for working with JUnit
- Host: GitHub
- URL: https://github.com/oswaldobapvicjr/junit-utils
- Owner: oswaldobapvicjr
- License: apache-2.0
- Created: 2020-03-30T19:34:10.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-31T06:23:16.000Z (3 months ago)
- Last Synced: 2024-10-31T07:20:39.285Z (3 months ago)
- Topics: assertion, instantiation, java, junit, junit-utils, testing, testing-tool, unit-testing
- Language: Java
- Homepage:
- Size: 148 KB
- Stars: 15
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
![junit-utils logo](resources/junit-utils_logo.svg)
[![Known Vulnerabilities](https://snyk.io/test/github/oswaldobapvicjr/junit-utils/badge.svg)](https://snyk.io/test/github/oswaldobapvicjr/junit-utils)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/oswaldobapvicjr/junit-utils/maven.yml?branch=master&label=build)](https://github.com/oswaldobapvicjr/junit-utils/actions/workflows/maven.yml)
[![Coverage](https://img.shields.io/codecov/c/github/oswaldobapvicjr/junit-utils)](https://codecov.io/gh/oswaldobapvicjr/junit-utils)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/net.obvj/junit-utils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/net.obvj/junit-utils)
[![Javadoc](https://javadoc.io/badge2/net.obvj/junit-utils/javadoc.svg)](https://javadoc.io/doc/net.obvj/junit-utils)Common utilities for working with JUnit:
- assertion of **exceptions**, as well as exception details, such as message and cause
- assertion of strings contents
- testing that a class **cannot be instantiated**----
## Examples
> **Note:** Consider the following static import declarations for readability:
```java
import static net.obvj.junit.utils.matchers.AdvancedMatchers.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
```### Asserting exceptions
The following assertion is true if the examined method throws a NullPointerException:
```java
assertThat(() -> myObject.doStuff(null),
throwsException(NullPointerException.class));
```To test the exception **message**, add `withMessageContaining` ...
```java
assertThat(() -> myObject.doStuff(null),
throwsException(NullPointerException.class)
.withMessageContaining("ERR-120008"));
```... or combine a String matcher:
````java
assertThat(() -> agent.loadSchemaFile("bad-schema.xsd"),
throwsException(AgentConfigurationException.class)
.withMessage(
either(startsWith("ERR-0001"))
.or(containsAny("invalid schema").ignoreCase())));
````If required, you can also test the exception **cause**:
```java
assertThat(() -> myObject.doStuff(null),
throwsException(MyException.class).withMessageContaining("ERR-120008")
.withCause(NullPointerException.class));
```And more:
```java
assertThat(() -> myObject.doStuff(null),
throwsException(MyException.class).withMessageContaining("ERR-120008")
.withCause(
exception(NullPointerException.class)
.withMessage("stuff cannot be null")));
```### Testing that instantiation is not allowed
The following assertion is particularly useful for utility classes:
```java
assertThat(TestUtils.class, instantiationNotAllowed());
```A matching class shall have all constructors declared as private and throw an exception inside the default constructor.
### Testing the contents of a string
The following examples represent some successful assertions using the Advanced String matcher:
```java
assertThat("The quick brown fox jumps over the lazy dog", containsAll("fox", "dog"));
assertThat("The quick brown fox jumps over the lazy dog", containsAny("FOX", "dragon").ignoreCase());
assertThat("The quick brown fox jumps over the lazy dog", containsNone("centaur"));
```### Testing numbers
Sometimes, it's more meaningful to check whether a number is positive or negative than testing the value itself, especially in situations where the exact value is unpredictable:
```java
assertThat(stopwatch.elapsedTime(), isPositive());
assertThat(duration.compareTo(otherDuration), isNegative());
```----
## How to include it
If you are using Maven, add **junit-utils** as a dependency in your pom.xml file:
```xml
net.obvj
junit-utils
1.8.0```
If you use other dependency management systems (such as Gradle, Grape, Ivy, etc.) click [here](https://maven-badges.herokuapp.com/maven-central/net.obvj/junit-utils).