Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 860453b

Browse files
Toxic DreamzToxic Dreamz
authored andcommitted
Fixed JUnit tests causing build issues due to mixing JUnit 4 & JUnit 5
1 parent 6921b0d commit 860453b

File tree

6 files changed

+52
-52
lines changed

6 files changed

+52
-52
lines changed

dirty-flag/src/test/java/org/dirty/flag/DirtyFlagTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,27 @@
2323

2424
package org.dirty.flag;
2525

26-
import static org.junit.jupiter.api.Assertions.assertFalse;
27-
import static org.junit.jupiter.api.Assertions.assertTrue;
28-
2926
import com.iluwatar.dirtyflag.DataFetcher;
27+
import org.junit.jupiter.api.Assertions;
3028
import org.junit.jupiter.api.Test;
3129

3230
/**
3331
* Application test
3432
*/
35-
public class DirtyFlagTest {
33+
class DirtyFlagTest {
3634

3735
@Test
38-
public void testIsDirty() {
36+
void testIsDirty() {
3937
var df = new DataFetcher();
4038
var countries = df.fetch();
41-
assertFalse(countries.isEmpty());
39+
Assertions.assertTrue(countries.isEmpty());
4240
}
4341

4442
@Test
45-
public void testIsNotDirty() {
43+
void testIsNotDirty() {
4644
var df = new DataFetcher();
4745
df.fetch();
4846
var countries = df.fetch();
49-
assertTrue(countries.isEmpty());
47+
Assertions.assertTrue(countries.isEmpty());
5048
}
5149
}

partial-response/pom.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,21 @@
3535

3636
<artifactId>partial-response</artifactId>
3737
<dependencies>
38-
<dependency>
39-
<groupId>junit</groupId>
40-
<artifactId>junit</artifactId>
41-
</dependency>
4238
<dependency>
4339
<groupId>org.junit.vintage</groupId>
4440
<artifactId>junit-vintage-engine</artifactId>
41+
<version>5.4.0</version>
4542
<scope>test</scope>
4643
</dependency>
4744
<dependency>
48-
<groupId>org.junit.jupiter</groupId>
49-
<artifactId>junit-jupiter-engine</artifactId>
45+
<groupId>org.mockito</groupId>
46+
<artifactId>mockito-junit-jupiter</artifactId>
5047
<scope>test</scope>
5148
</dependency>
5249
<dependency>
53-
<groupId>org.mockito</groupId>
54-
<artifactId>mockito-core</artifactId>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter-engine</artifactId>
52+
<scope>test</scope>
5553
</dependency>
5654
</dependencies>
5755
<build>

partial-response/src/test/java/com/iluwatar/partialresponse/AppTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323

2424
package com.iluwatar.partialresponse;
2525

26-
import org.junit.Test;
27-
28-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.Assertions;
2928

3029
/**
3130
* Application test
3231
*/
33-
public class AppTest {
32+
class AppTest {
3433

3534
@Test
36-
public void shouldExecuteApplicationWithoutException() {
37-
assertDoesNotThrow(() -> App.main(new String[]{}));
35+
void shouldExecuteApplicationWithoutException() {
36+
Assertions.assertDoesNotThrow(() -> App.main(new String[]{}));
3837
}
3938

4039
}

partial-response/src/test/java/com/iluwatar/partialresponse/FieldJsonMapperTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,23 @@
2323

2424
package com.iluwatar.partialresponse;
2525

26-
import static org.junit.Assert.assertEquals;
27-
28-
import org.junit.Before;
29-
import org.junit.Test;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Test;
3029

3130
/**
3231
* tests {@link FieldJsonMapper}.
3332
*/
34-
public class FieldJsonMapperTest {
35-
private FieldJsonMapper mapper;
33+
class FieldJsonMapperTest {
34+
private static FieldJsonMapper mapper;
3635

37-
@Before
38-
public void setUp() {
36+
@BeforeAll
37+
static void setUp() {
3938
mapper = new FieldJsonMapper();
4039
}
4140

4241
@Test
43-
public void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
42+
void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
4443
var fields = new String[]{"id", "title", "length"};
4544
var video = new Video(
4645
2, "Godzilla Resurgence", 120,
@@ -50,6 +49,6 @@ public void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
5049
var jsonFieldResponse = mapper.toJson(video, fields);
5150

5251
var expectedDetails = "{\"id\": 2,\"title\": \"Godzilla Resurgence\",\"length\": 120}";
53-
assertEquals(expectedDetails, jsonFieldResponse);
52+
Assertions.assertEquals(expectedDetails, jsonFieldResponse);
5453
}
5554
}

partial-response/src/test/java/com/iluwatar/partialresponse/VideoResourceTest.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,29 @@
2323

2424
package com.iluwatar.partialresponse;
2525

26-
import static org.junit.Assert.assertEquals;
27-
import static org.mockito.Matchers.any;
28-
import static org.mockito.Matchers.eq;
29-
import static org.mockito.Mockito.when;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.ExtendWith;
30+
import org.mockito.Matchers;
31+
import org.mockito.Mock;
32+
import org.mockito.Mockito;
33+
import org.mockito.junit.jupiter.MockitoExtension;
3034

3135
import java.util.Map;
32-
import org.junit.Before;
33-
import org.junit.Test;
34-
import org.junit.runner.RunWith;
35-
import org.mockito.Mock;
36-
import org.mockito.runners.MockitoJUnitRunner;
3736

3837
/**
3938
* tests {@link VideoResource}.
4039
*/
41-
@RunWith(MockitoJUnitRunner.class)
42-
public class VideoResourceTest {
40+
@ExtendWith(MockitoExtension.class)
41+
class VideoResourceTest {
4342
@Mock
44-
private FieldJsonMapper fieldJsonMapper;
43+
private static FieldJsonMapper fieldJsonMapper;
4544

46-
private VideoResource resource;
45+
private static VideoResource resource;
4746

48-
@Before
49-
public void setUp() {
47+
@BeforeAll
48+
static void setUp() {
5049
var videos = Map.of(
5150
1, new Video(1, "Avatar", 178, "epic science fiction film",
5251
"James Cameron", "English"),
@@ -58,23 +57,23 @@ public void setUp() {
5857
}
5958

6059
@Test
61-
public void shouldGiveVideoDetailsById() throws Exception {
60+
void shouldGiveVideoDetailsById() throws Exception {
6261
var actualDetails = resource.getDetails(1);
6362

6463
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
6564
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
66-
assertEquals(expectedDetails, actualDetails);
65+
Assertions.assertEquals(expectedDetails, actualDetails);
6766
}
6867

6968
@Test
70-
public void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
69+
void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
7170
var fields = new String[]{"id", "title", "length"};
7271

7372
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178}";
74-
when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
73+
Mockito.when(fieldJsonMapper.toJson(Matchers.any(Video.class), Matchers.eq(fields))).thenReturn(expectedDetails);
7574

7675
var actualFieldsDetails = resource.getDetails(2, fields);
7776

78-
assertEquals(expectedDetails, actualFieldsDetails);
77+
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
7978
}
8079
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<annotation-api.version>1.3.2</annotation-api.version>
5555
<system-rules.version>1.19.0</system-rules.version>
5656
<urm.version>1.4.8</urm.version>
57+
<mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version>
5758
<!-- SonarCloud -->
5859
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
5960
<sonar.organization>iluwatar</sonar.organization>
@@ -226,6 +227,12 @@
226227
<artifactId>spring-webmvc</artifactId>
227228
<version>${spring.version}</version>
228229
</dependency>
230+
<dependency>
231+
<groupId>org.mockito</groupId>
232+
<artifactId>mockito-junit-jupiter</artifactId>
233+
<version>${mockito-junit-jupiter.version}</version>
234+
<scope>test</scope>
235+
</dependency>
229236
<dependency>
230237
<groupId>com.h2database</groupId>
231238
<artifactId>h2</artifactId>

0 commit comments

Comments
 (0)