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;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.dirtyflag.DataFetcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Application test
*/
public class DirtyFlagTest {
class DirtyFlagTest {
@Test
public void testIsDirty() {
void testIsDirty() {
var df = new DataFetcher();
var countries = df.fetch();
assertFalse(countries.isEmpty());
Assertions.assertTrue(countries.isEmpty());
}
@Test
public void testIsNotDirty() {
void testIsNotDirty() {
var df = new DataFetcher();
df.fetch();
var countries = df.fetch();
assertTrue(countries.isEmpty());
Assertions.assertTrue(countries.isEmpty());
}
}

View File

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

View File

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

View File

@ -23,24 +23,23 @@
package com.iluwatar.partialresponse;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* tests {@link FieldJsonMapper}.
*/
public class FieldJsonMapperTest {
private FieldJsonMapper mapper;
class FieldJsonMapperTest {
private static FieldJsonMapper mapper;
@Before
public void setUp() {
@BeforeAll
static void setUp() {
mapper = new FieldJsonMapper();
}
@Test
public void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
var fields = new String[]{"id", "title", "length"};
var video = new Video(
2, "Godzilla Resurgence", 120,
@ -50,6 +49,6 @@ public class FieldJsonMapperTest {
var jsonFieldResponse = mapper.toJson(video, fields);
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;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
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 org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* tests {@link VideoResource}.
*/
@RunWith(MockitoJUnitRunner.class)
public class VideoResourceTest {
@ExtendWith(MockitoExtension.class)
class VideoResourceTest {
@Mock
private FieldJsonMapper fieldJsonMapper;
private static FieldJsonMapper fieldJsonMapper;
private VideoResource resource;
private static VideoResource resource;
@Before
public void setUp() {
@BeforeAll
static void setUp() {
var videos = Map.of(
1, new Video(1, "Avatar", 178, "epic science fiction film",
"James Cameron", "English"),
@ -58,23 +57,23 @@ public class VideoResourceTest {
}
@Test
public void shouldGiveVideoDetailsById() throws Exception {
void shouldGiveVideoDetailsById() throws Exception {
var actualDetails = resource.getDetails(1);
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
assertEquals(expectedDetails, actualDetails);
Assertions.assertEquals(expectedDetails, actualDetails);
}
@Test
public void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
var fields = new String[]{"id", "title", "length"};
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);
assertEquals(expectedDetails, actualFieldsDetails);
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
}
}

View File

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