use Junit5 instead of TestNG

We want to be able to use @SpringBootTest tests that fully initialize
the Spring application. This is much easier done with Junit than TestNG.
Gradle does not support (at least not easily) to run Junit and TestNG
tests. Therefore we switch to Junit with all tests.
The original reason for using TestNG was that Junit didn't support
data providers. But that finally changed in Junit5 with
ParameterizedTest.
This commit is contained in:
2019-12-13 14:33:20 +01:00
parent 394e16ad27
commit 07ad62ddd9
26 changed files with 660 additions and 837 deletions

View File

@@ -5,33 +5,35 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.Assertions;
public class DateTimeRangeTest {
@DataProvider
Object[][] providerIntersect() {
final List<Object[]> result = new ArrayList<>();
static Stream<Arguments> providerIntersect() {
final List<Arguments> result = new ArrayList<>();
final OffsetDateTime a = Instant.ofEpochMilli(1000).atOffset(ZoneOffset.UTC);
final OffsetDateTime b = Instant.ofEpochMilli(2000).atOffset(ZoneOffset.UTC);
final OffsetDateTime c = Instant.ofEpochMilli(3000).atOffset(ZoneOffset.UTC);
final OffsetDateTime d = Instant.ofEpochMilli(4000).atOffset(ZoneOffset.UTC);
result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(c, d), false });
result.add(new Object[] { new DateTimeRange(a, c), new DateTimeRange(b, d), true });
result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true });
result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true });
result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(b, d), true });
result.add(Arguments.of(new DateTimeRange(a, b), new DateTimeRange(c, d), false));
result.add(Arguments.of(new DateTimeRange(a, c), new DateTimeRange(b, d), true));
result.add(Arguments.of(new DateTimeRange(a, d), new DateTimeRange(b, d), true));
result.add(Arguments.of(new DateTimeRange(a, d), new DateTimeRange(b, d), true));
result.add(Arguments.of(new DateTimeRange(a, b), new DateTimeRange(b, d), true));
return result.toArray(new Object[result.size()][]);
return result.stream();
}
@Test(dataProvider = "providerIntersect")
@ParameterizedTest
@MethodSource("providerIntersect")
public void testIntersect(final DateTimeRange a, final DateTimeRange b, final boolean expected) throws Exception {
Assert.assertEquals(a.intersect(b), expected, a + " intersects " + b);
Assert.assertEquals(b.intersect(a), expected, a + " intersects " + b);
Assertions.assertEquals(expected, a.intersect(b), a + " intersects " + b);
Assertions.assertEquals(expected, b.intersect(a), a + " intersects " + b);
}
}

View File

@@ -10,26 +10,26 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class StringCompressorTest {
private Path dataDirectory;
@BeforeMethod
@BeforeEach
public void beforeMethod() throws IOException {
dataDirectory = Files.createTempDirectory("pdb");
}
@AfterMethod
@AfterEach
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
}
@Test
public void testKeyCompressorRoundtrip() throws Exception {
final StringCompressor keyValueCompressor = StringCompressor.create(dataDirectory.resolve("key.csv"));
@@ -37,9 +37,10 @@ public class StringCompressorTest {
final Integer intFoo = keyValueCompressor.put(value);
final String actual = keyValueCompressor.get(intFoo);
Assert.assertEquals(actual, value);
Assertions.assertEquals(value, actual);
}
@Test
public void testKeyCompressorInitialization() throws Exception {
final Path database = dataDirectory.resolve("key.csv");
final String value = "foo";
@@ -56,7 +57,7 @@ public class StringCompressorTest {
}
@Test(invocationCount = 1)
@Test
public void testPutConcurrently() throws InterruptedException, ExecutionException {
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs();
final StringCompressor stringCompressor = new StringCompressor(usip);
@@ -75,6 +76,6 @@ public class StringCompressorTest {
pool.shutdown();
pool.awaitTermination(1, TimeUnit.MILLISECONDS);
Assert.assertEquals((int) usip.getHighestInteger(), 3 * numEntries - 1);
Assertions.assertEquals(3 * numEntries - 1, (int) usip.getHighestInteger());
}
}

View File

@@ -4,27 +4,27 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class UniqueStringIntegerPairsTest {
private Path dataDirectory;
@BeforeMethod
@BeforeEach
public void beforeMethod() throws IOException {
dataDirectory = Files.createTempDirectory("pdb");
}
@AfterMethod
@AfterEach
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
}
@Test
public void testPutGet() throws Exception {
final Path database = dataDirectory.resolve("key.csv");
final String first = "key1";
@@ -34,18 +34,19 @@ public class UniqueStringIntegerPairsTest {
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
usip.put(first, second);
Assert.assertEquals(usip.get(first), second);
Assert.assertEquals(usip.getKey(second), first);
Assertions.assertEquals(second, usip.get(first));
Assertions.assertEquals(first, usip.getKey(second));
}
{
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
Assert.assertEquals(usip.get(first), second);
Assert.assertEquals(usip.getKey(second), first);
Assertions.assertEquals(second, usip.get(first));
Assertions.assertEquals(first, usip.getKey(second));
}
}
@Test
public void testUniqueKeyContstraint() throws Exception {
final Path database = dataDirectory.resolve("key.csv");
final String first = "key1";
@@ -57,7 +58,7 @@ public class UniqueStringIntegerPairsTest {
// cannot add another pair with the first key
final int another = second + 1;
usip.put(first, another);
Assert.fail("expected an IllegalArgumentException");
Assertions.fail("expected an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
@@ -66,7 +67,7 @@ public class UniqueStringIntegerPairsTest {
// cannot add another pair with the same second value
final String another = first + 1;
usip.put(another, second);
Assert.fail("expected an IllegalArgumentException");
Assertions.fail("expected an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}