Skip to content

Commit 0403116

Browse files
committed
Replace Hamcrest example with AssertJ in User Guide
This commit also adds a "tip" about using AssertJ's Assumptions support. Closes #5050
1 parent 16a6289 commit 0403116

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -513,33 +513,22 @@ include::{kotlinTestDir}/example/KotlinAssertionsDemo.kt[tags=user_guide]
513513
==== Third-party Assertion Libraries
514514

515515
Even though the assertion facilities provided by JUnit Jupiter are sufficient for many
516-
testing scenarios, there are times when more power and additional functionality such as
517-
_matchers_ are desired or required. In such cases, the JUnit team recommends the use of
518-
third-party assertion libraries such as {AssertJ}, {Hamcrest}, {Truth}, etc. Developers
519-
are therefore free to use the assertion library of their choice.
516+
testing scenarios, there are times when more power and additional functionality are
517+
desired or required. In such cases, the JUnit team recommends the use of third-party
518+
assertion libraries such as {AssertJ}, {Hamcrest}, {Truth}, etc. Developers are therefore
519+
free to use the assertion library of their choice.
520520

521-
For example, the combination of _matchers_ and a fluent API can be used to make
522-
assertions more descriptive and readable. However, JUnit Jupiter's `{Assertions}` class
523-
does not provide an
524-
https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat[`assertThat()`]
525-
method like the one found in JUnit 4's `org.junit.Assert` class which accepts a Hamcrest
526-
https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html[`Matcher`]. Instead,
527-
developers are encouraged to use the built-in support for matchers provided by third-party
528-
assertion libraries.
529-
530-
The following example demonstrates how to use the `assertThat()` support from Hamcrest in
531-
a JUnit Jupiter test. As long as the Hamcrest library has been added to the classpath,
532-
you can statically import methods such as `assertThat()`, `is()`, and `equalTo()` and
533-
then use them in tests like in the `assertWithHamcrestMatcher()` method below.
521+
For example, the following demonstrates how to use the `assertThat()` support from AssertJ
522+
in a JUnit Jupiter test. As long as the AssertJ library has been added to the classpath,
523+
you can statically import methods such as `assertThat()`, `assertThatException()`, etc.
524+
from `org.assertj.core.api.Assertions` and then use them in tests like in the
525+
`assertWithAssertJ()` method below.
534526

535527
[source,java,indent=0]
536528
----
537-
include::{testDir}/example/HamcrestAssertionsDemo.java[tags=user_guide]
529+
include::{testDir}/example/AssertJAssertionsDemo.java[tags=user_guide]
538530
----
539531

540-
Naturally, legacy tests based on the JUnit 4 programming model can continue using
541-
`org.junit.Assert#assertThat`.
542-
543532
[TIP]
544533
.Excluding Jupiter’s Assertions From a Project’s Classpath
545534
====
@@ -594,6 +583,11 @@ NOTE: It is also possible to use methods from JUnit 4's `org.junit.Assume` class
594583
assumptions. Specifically, JUnit Jupiter supports JUnit 4's `AssumptionViolatedException`
595584
to signal that a test should be aborted instead of marked as a failure.
596585

586+
TIP: If you use AssertJ for assertions, you may also wish to use AssertJ for assumptions.
587+
To do so, you can statically import the `assumeThat()` method from
588+
`org.assertj.core.api.Assumptions` and then use AssertJ's fluent API to specify your
589+
assumptions.
590+
597591
[[writing-tests-exceptions]]
598592
=== Exception Handling
599593

documentation/src/test/java/example/HamcrestAssertionsDemo.java renamed to documentation/src/test/java/example/AssertJAssertionsDemo.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@
1111
package example;
1212

1313
// tag::user_guide[]
14-
import static org.hamcrest.CoreMatchers.equalTo;
15-
import static org.hamcrest.CoreMatchers.is;
16-
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThat;
1715

1816
import example.util.Calculator;
1917

2018
import org.junit.jupiter.api.Test;
2119

22-
class HamcrestAssertionsDemo {
20+
class AssertJAssertionsDemo {
2321

2422
private final Calculator calculator = new Calculator();
2523

2624
@Test
27-
void assertWithHamcrestMatcher() {
28-
assertThat(calculator.subtract(4, 1), is(equalTo(3)));
25+
void assertWithAssertJ() {
26+
assertThat(calculator.subtract(4, 1)).isEqualTo(3);
2927
}
3028

3129
}

0 commit comments

Comments
 (0)