Skip to content

Commit 8cb0a39

Browse files
committed
add junit 5 example.
1 parent e26e608 commit 8cb0a39

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

exercise-5.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@ In the `src/main/test` folder the file BootiqueControllerTest should be availabl
1010

1111
We are going to be using a mocking framework to mock out our dependencies. Spring test conveniently bundles Mockito so let's use that. First thing to do now, is to define the MockitoRunner as the testrunner for your unit test. Add it now.
1212

13-
In java you would do something like the listing below.
13+
In java you would do something like the listing below.
14+
15+
For JUnit 4
1416

1517
```java
18+
import org.junit.Test
19+
import org.junit.runner.RunWith
20+
import org.mockito.junit.MockitoJUnitRunner
21+
1622
@RunWith(MockitoJUnitRunner.class)
1723
```
1824

19-
**Exercise**: Convert the test to Kotlin and add the MockitoJUnitRunner to it.
25+
For JUnit 5
26+
27+
```java
28+
import org.junit.jupiter.api.Test
29+
import org.junit.jupiter.api.extension.ExtendWith
30+
import org.mockito.junit.jupiter.MockitoExtension
31+
32+
@ExtendWith(MockitoExtension::class)
33+
```
34+
35+
**Exercise**: Convert the test to Kotlin and add Mockito to it.
2036

2137
<details>
2238
<summary>Suggested solution</summary>
@@ -92,12 +108,9 @@ public void testRetrieveBasket() {
92108

93109
```
94110
import org.assertj.core.api.Assertions.assertThat
95-
import org.junit.Test
96-
import org.junit.runner.RunWith
97111
import org.mockito.InjectMocks
98112
import org.mockito.Mock
99113
import org.mockito.Mockito.*
100-
import org.mockito.junit.MockitoJUnitRunner
101114
```
102115

103116
<details>
@@ -177,7 +190,17 @@ We are going to test the app by calling an endpoint, so we'll be modifying the t
177190

178191
First, we'll need to configure the web environment to test against. Modify the `@SpringBootTest` annotation like below.
179192

193+
For JUnit 4
194+
195+
```java
196+
@RunWith(SpringRunner::class)
197+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
198+
```
199+
200+
For JUnit 5
201+
180202
```java
203+
@ExtendWith(SpringExtension::class)
181204
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
182205
```
183206

0 commit comments

Comments
 (0)