Fixed JUnit tests causing build issues due to mixing JUnit 4 & JUnit 5

This commit is contained in:
Toxic Dreamz 2020-08-19 23:14:37 +04:00
parent 6921b0dce0
commit 860453b46b
6 changed files with 52 additions and 52 deletions

View File

@ -23,29 +23,27 @@
package org.dirty.flag; package org.dirty.flag;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.dirtyflag.DataFetcher; import com.iluwatar.dirtyflag.DataFetcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
* Application test * Application test
*/ */
public class DirtyFlagTest { class DirtyFlagTest {
@Test @Test
public void testIsDirty() { void testIsDirty() {
var df = new DataFetcher(); var df = new DataFetcher();
var countries = df.fetch(); var countries = df.fetch();
assertFalse(countries.isEmpty()); Assertions.assertTrue(countries.isEmpty());
} }
@Test @Test
public void testIsNotDirty() { void testIsNotDirty() {
var df = new DataFetcher(); var df = new DataFetcher();
df.fetch(); df.fetch();
var countries = df.fetch(); var countries = df.fetch();
assertTrue(countries.isEmpty()); Assertions.assertTrue(countries.isEmpty());
} }
} }

View File

@ -35,13 +35,15 @@
<artifactId>partial-response</artifactId> <artifactId>partial-response</artifactId>
<dependencies> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.junit.vintage</groupId> <groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId> <artifactId>junit-vintage-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -49,10 +51,6 @@
<artifactId>junit-jupiter-engine</artifactId> <artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -23,18 +23,17 @@
package com.iluwatar.partialresponse; package com.iluwatar.partialresponse;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/** /**
* Application test * Application test
*/ */
public class AppTest { class AppTest {
@Test @Test
public void shouldExecuteApplicationWithoutException() { void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{})); Assertions.assertDoesNotThrow(() -> App.main(new String[]{}));
} }
} }

View File

@ -23,24 +23,23 @@
package com.iluwatar.partialresponse; package com.iluwatar.partialresponse;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Assertions;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
/** /**
* tests {@link FieldJsonMapper}. * tests {@link FieldJsonMapper}.
*/ */
public class FieldJsonMapperTest { class FieldJsonMapperTest {
private FieldJsonMapper mapper; private static FieldJsonMapper mapper;
@Before @BeforeAll
public void setUp() { static void setUp() {
mapper = new FieldJsonMapper(); mapper = new FieldJsonMapper();
} }
@Test @Test
public void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception { void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
var fields = new String[]{"id", "title", "length"}; var fields = new String[]{"id", "title", "length"};
var video = new Video( var video = new Video(
2, "Godzilla Resurgence", 120, 2, "Godzilla Resurgence", 120,
@ -50,6 +49,6 @@ public class FieldJsonMapperTest {
var jsonFieldResponse = mapper.toJson(video, fields); var jsonFieldResponse = mapper.toJson(video, fields);
var expectedDetails = "{\"id\": 2,\"title\": \"Godzilla Resurgence\",\"length\": 120}"; var expectedDetails = "{\"id\": 2,\"title\": \"Godzilla Resurgence\",\"length\": 120}";
assertEquals(expectedDetails, jsonFieldResponse); Assertions.assertEquals(expectedDetails, jsonFieldResponse);
} }
} }

View File

@ -23,30 +23,29 @@
package com.iluwatar.partialresponse; package com.iluwatar.partialresponse;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Assertions;
import static org.mockito.Matchers.any; import org.junit.jupiter.api.BeforeAll;
import static org.mockito.Matchers.eq; import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.when; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Map; import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/** /**
* tests {@link VideoResource}. * tests {@link VideoResource}.
*/ */
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class VideoResourceTest { class VideoResourceTest {
@Mock @Mock
private FieldJsonMapper fieldJsonMapper; private static FieldJsonMapper fieldJsonMapper;
private VideoResource resource; private static VideoResource resource;
@Before @BeforeAll
public void setUp() { static void setUp() {
var videos = Map.of( var videos = Map.of(
1, new Video(1, "Avatar", 178, "epic science fiction film", 1, new Video(1, "Avatar", 178, "epic science fiction film",
"James Cameron", "English"), "James Cameron", "English"),
@ -58,23 +57,23 @@ public class VideoResourceTest {
} }
@Test @Test
public void shouldGiveVideoDetailsById() throws Exception { void shouldGiveVideoDetailsById() throws Exception {
var actualDetails = resource.getDetails(1); var actualDetails = resource.getDetails(1);
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": " var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}"; + "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
assertEquals(expectedDetails, actualDetails); Assertions.assertEquals(expectedDetails, actualDetails);
} }
@Test @Test
public void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception { void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
var fields = new String[]{"id", "title", "length"}; var fields = new String[]{"id", "title", "length"};
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178}"; var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178}";
when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails); Mockito.when(fieldJsonMapper.toJson(Matchers.any(Video.class), Matchers.eq(fields))).thenReturn(expectedDetails);
var actualFieldsDetails = resource.getDetails(2, fields); var actualFieldsDetails = resource.getDetails(2, fields);
assertEquals(expectedDetails, actualFieldsDetails); Assertions.assertEquals(expectedDetails, actualFieldsDetails);
} }
} }

View File

@ -54,6 +54,7 @@
<annotation-api.version>1.3.2</annotation-api.version> <annotation-api.version>1.3.2</annotation-api.version>
<system-rules.version>1.19.0</system-rules.version> <system-rules.version>1.19.0</system-rules.version>
<urm.version>1.4.8</urm.version> <urm.version>1.4.8</urm.version>
<mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version>
<!-- SonarCloud --> <!-- SonarCloud -->
<sonar.host.url>https://sonarcloud.io</sonar.host.url> <sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.organization>iluwatar</sonar.organization> <sonar.organization>iluwatar</sonar.organization>
@ -226,6 +227,12 @@
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito-junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>