Skip to content

Commit b8a2b83

Browse files
committed
Upgrade examples from JUnit 3 -> JUnit 5
1 parent 3626acd commit b8a2b83

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

docs/tutorial.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ Hamcrest is a framework for writing matcher objects allowing 'match' rules to be
1212
When writing tests it is sometimes difficult to get the balance right between overspecifying the test (and making it brittle to changes), and not specifying enough (making the test less valuable since it continues to pass even when the thing being tested is broken). Having a tool that allows you to pick out precisely the aspect under test and describe the values it should have, to a controlled level of precision, helps greatly in writing tests that are "just right". Such tests fail when the behaviour of the aspect under test deviates from the expected behaviour, yet continue to pass when minor, unrelated changes to the behaviour are made.
1313

1414
### My first Hamcrest test
15-
We'll start by writing a very simple JUnit 3 test, but instead of using JUnit's `assertEquals` methods, we use Hamcrest's `assertThat` construct and the standard set of matchers, both of which we statically import:
15+
We'll start by writing a very simple JUnit 5 test, but instead of using JUnit's `assertEquals` methods, we use Hamcrest's `assertThat` construct and the standard set of matchers, both of which we statically import:
1616

1717
```java
18+
import org.junit.jupiter.api.Test;
1819
import static org.hamcrest.MatcherAssert.assertThat;
1920
import static org.hamcrest.Matchers.*;
20-
import junit.framework.TestCase;
2121

22-
public class BiscuitTest extends TestCase {
22+
public class BiscuitTest {
23+
@Test
2324
public void testEquals() {
2425
Biscuit theBiscuit = new Biscuit("Ginger");
2526
Biscuit myBiscuit = new Biscuit("Ginger");
@@ -39,7 +40,7 @@ assertThat("hazelnuts", theBiscuit.getHazelnutCount(), equalTo(3));
3940
```
4041

4142
### Other test frameworks
42-
Hamcrest has been designed from the outset to integrate with different unit testing frameworks. For example, Hamcrest can be used with JUnit 3 and 4 and TestNG. (For details have a look at the examples that come with the full Hamcrest distribution.) It is easy enough to migrate to using Hamcrest-style assertions in an existing test suite, since other assertion styles can co-exist with Hamcrest's.
43+
Hamcrest has been designed from the outset to integrate with different unit testing frameworks. For example, Hamcrest can be used with JUnit (all versions) and TestNG. (For details have a look at the examples that come with the full Hamcrest distribution.) It is easy enough to migrate to using Hamcrest-style assertions in an existing test suite, since other assertion styles can co-exist with Hamcrest's.
4344

4445
Hamcrest can also be used with mock objects frameworks by using adaptors to bridge from the mock objects framework's concept of a matcher to a Hamcrest matcher. For example, JMock 1's constraints are Hamcrest's matchers. Hamcrest provides a JMock 1 adaptor to allow you to use Hamcrest matchers in your JMock 1 tests. JMock 2 doesn't need such an adaptor layer since it is designed to use Hamcrest as its matching library. Hamcrest also provides adaptors for EasyMock 2. Again, see the Hamcrest examples for more details.
4546

@@ -111,6 +112,7 @@ Hamcrest comes bundled with lots of useful matchers, but you'll probably find th
111112
Let's write our own matcher for testing if a double value has the value NaN (not a number). This is the test we want to write:
112113

113114
```java
115+
@Test
114116
public void testSquareRootOfMinusOneIsNotANumber() {
115117
assertThat(Math.sqrt(-1), is(notANumber()));
116118
}
@@ -122,7 +124,6 @@ And here's the implementation:
122124
package org.hamcrest.examples.tutorial;
123125

124126
import org.hamcrest.Description;
125-
import org.hamcrest.Factory;
126127
import org.hamcrest.Matcher;
127128
import org.hamcrest.TypeSafeMatcher;
128129

@@ -158,15 +159,13 @@ java.lang.AssertionError: Expected: is not a number got : <1.0>
158159
The third method in our matcher is a convenience factory method. We statically import this method to use the matcher in our test:
159160

160161
```java
162+
import org.junit.jupiter.api.Test;
161163
import static org.hamcrest.MatcherAssert.assertThat;
162164
import static org.hamcrest.Matchers.*;
163-
164165
import static org.hamcrest.examples.tutorial.IsNotANumber.notANumber;
165166

166-
import junit.framework.TestCase;
167-
168-
public class NumberTest extends TestCase {
169-
167+
public class NumberTest {
168+
@Test
170169
public void testSquareRootOfMinusOneIsNotANumber() {
171170
assertThat(Math.sqrt(-1), is(notANumber()));
172171
}

0 commit comments

Comments
 (0)