https://github.com/hpple/reflection-assert
Reflection Assert for JUnit 5 & others
https://github.com/hpple/reflection-assert
assert assertions java junit junit5 reflection testing unit-testing
Last synced: 2 months ago
JSON representation
Reflection Assert for JUnit 5 & others
- Host: GitHub
- URL: https://github.com/hpple/reflection-assert
- Owner: hpple
- License: apache-2.0
- Created: 2017-11-26T22:29:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-08-22T23:35:51.000Z (over 4 years ago)
- Last Synced: 2023-10-06T09:12:05.640Z (over 2 years ago)
- Topics: assert, assertions, java, junit, junit5, reflection, testing, unit-testing
- Language: Java
- Homepage:
- Size: 249 KB
- Stars: 9
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Reflection Assert
=======
[](https://maven-badges.herokuapp.com/maven-central/com.github.hpple.reflection-assert/reflection-assert/)
[](https://app.travis-ci.com/github/hpple/reflection-assert)
Fork of reflection assert from Unitils (http://www.unitils.org) compatible with JUnit 5 & other testing tools that support [opentest4j](https://github.com/ota4j-team/opentest4j).
Usage
----------------
#### reflection-assert
Assert that two objects are equal using reflection for field-by-field comparision:
```
assertReflectiveThat(actual).isEqualTo(expected);
```
Same, but ignoring order for collections:
```
assertLenientThat(actual).isEqualTo(expected);
```
Or assert that two objects are not equal providing custom message:
```
assertReflective().withMessage("message").that(actual).isNotEqualTo(unexpected);
```
A report for a failed assertion will look like:
```
Expected: User>
Actual: User>
--- Found following differences ---
address.street: expected: "2th", actual: "1th"
title: expected: "CTO", actual: "Officer"
--- Difference detail tree ---
expected: User>
actual: User>
address expected: Address
address actual: Address
address.street expected: "2th"
address.street actual: "1th"
title expected: "CTO"
title actual: "Officer"
```
#### reflection-assert-vintage
Provides reflection assertions in conformance with vintage Unitils API for smooth migration.
You may treat this module as a drop-in replacement for Unitils if the reflection assert was the only package used from Unitils in your system.
#### reflection-comparator
Additionally, you may want to compare two objects reflectively out of the test scope without getting dependency on JUnit and other test-related libs.
Or maybe just get the difference report without any assertions involved.
You may achieve it by depending on this module only & directly using ReflectionComparator that could be obtained with opt-in leniency modes:
```
Difference diff = ReflectionComparatorFactory.createReflectionComparator().getDifference(a, b)
```
```
ReflectionComparatorFactory.createReflectionComparator(LENIENT_ORDER, LENIENT_DATES).isEqual(a, b)
```
Reports could be generated in the same format as for assertions:
```
new DefaultDifferenceReport().createReport(difference)
```
Dependency
----------------
Releases are available in [Maven Central](https://repo1.maven.org/maven2/com/github/hpple/reflection-assert).
Just add the dependency:
#### Gradle
##### reflection-assert
```gradle
testCompile 'com.github.hpple.reflection-assert:reflection-assert:0.3'
```
##### reflection-assert-vintage
```gradle
testCompile 'com.github.hpple.reflection-assert:reflection-assert-vintage:0.3'
```
##### reflection-comparator
```gradle
compile 'com.github.hpple.reflection-assert:reflection-comparator:0.3'
```
or
```gradle
testCompile 'com.github.hpple.reflection-assert:reflection-comparator:0.3'
```
#### Maven
##### reflection-assert
```xml
com.github.hpple.reflection-assert
reflection-assert
0.3
test
```
##### reflection-assert-vintage
```xml
com.github.hpple.reflection-assert
reflection-assert-vintage
0.3
test
```
##### reflection-comparator
```xml
com.github.hpple.reflection-assert
reflection-comparator
0.3
```
or
```xml
com.github.hpple.reflection-assert
reflection-comparator
0.3
test
```