Skip to content

Commit

Permalink
JUnit 5 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob authored Dec 29, 2023
1 parent d96df51 commit fe71d01
Show file tree
Hide file tree
Showing 22 changed files with 336 additions and 165 deletions.
3 changes: 2 additions & 1 deletion common/diff/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ plugins {
}

dependencies {
testImplementation(libs.test.junit4)
testCompileOnly(libs.test.junit4)
testRuntimeOnly(libs.test.junit5.vintage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
65 changes: 40 additions & 25 deletions common/model/src/test/java/net/twisterrob/blt/model/LineTest.java
Original file line number Diff line number Diff line change
@@ -1,125 +1,140 @@
package net.twisterrob.blt.model;

import java.util.*;
import java.util.Map.Entry;

import org.junit.Test;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

public class LineTest {

@Test
public void testFixMapList() {
Map<Line, List<String>> map = new TreeMap<>();
assertEquals(0, map.size());
Map<Line, List<String>> newMap = Line.fixMap(map, Collections.<String>emptyList());
assertThat(map, anEmptyMap());
Map<Line, List<String>> newMap = Line.fixMap(map, emptyList());
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, List<String>> element : map.entrySet()) {
for (Map.Entry<Line, List<String>> element : map.entrySet()) {
assertNotNull(element.getKey());
List<String> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(Collections.emptyList(), element.getValue());
assertSame(emptyList(), element.getValue());
}
}

@Test
public void testFixMapListInstance() {
Map<Line, List<String>> map = new TreeMap<>();
LinkedList<String> empty = new LinkedList<>();
assertEquals(0, map.size());
assertThat(map, anEmptyMap());
Map<Line, List<String>> newMap = Line.fixMap(map, empty);
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, List<String>> element : map.entrySet()) {
for (Map.Entry<Line, List<String>> element : map.entrySet()) {
assertNotNull(element.getKey());
List<String> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(empty, element.getValue());
}
}

@Test
public void testFixMapLinkedList() {
HashMap<Line, LinkedList<String>> map = new HashMap<>();
LinkedList<String> empty = new LinkedList<>();
assertEquals(0, map.size());
assertThat(map, anEmptyMap());
HashMap<Line, LinkedList<String>> newMap = Line.fixMap(map, empty);
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, LinkedList<String>> element : map.entrySet()) {
for (Map.Entry<Line, LinkedList<String>> element : map.entrySet()) {
assertNotNull(element.getKey());
LinkedList<String> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(empty, value);
}
}

@Test
public void testFixMapSet() {
Map<Line, Set<String>> map = new TreeMap<>();
Set<String> empty = Collections.emptySet();
assertEquals(0, map.size());
Set<String> empty = emptySet();
assertThat(map, anEmptyMap());
Map<Line, Set<String>> newMap = Line.fixMap(map, empty);
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, Set<String>> element : map.entrySet()) {
for (Map.Entry<Line, Set<String>> element : map.entrySet()) {
assertNotNull(element.getKey());
Set<String> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(empty, element.getValue());
}
}

@Test
public void testFixMapSetInstance() {
Map<Line, Set<String>> map = new TreeMap<>();
Set<String> empty = Collections.emptySet();
assertEquals(0, map.size());
Set<String> empty = emptySet();
assertThat(map, anEmptyMap());
Map<Line, Set<String>> newMap = Line.fixMap(map, empty);
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, Set<String>> element : map.entrySet()) {
for (Map.Entry<Line, Set<String>> element : map.entrySet()) {
assertNotNull(element.getKey());
Set<String> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(empty, element.getValue());
}
}

@Test
public void testFixMapHashSet() {
HashMap<Line, HashSet<String>> map = new HashMap<>();
HashSet<String> empty = new HashSet<>();
assertEquals(0, map.size());
assertThat(map, anEmptyMap());
HashMap<Line, HashSet<String>> newMap = Line.fixMap(map, empty);
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, HashSet<String>> element : map.entrySet()) {
for (Map.Entry<Line, HashSet<String>> element : map.entrySet()) {
assertNotNull(element.getKey());
HashSet<String> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(empty, value);
}
}

@Test
public void testFixMapMap() {
Map<Line, Map<String, Double>> map = new TreeMap<>();
assertEquals(0, map.size());
Map<Line, Map<String, Double>> newMap = Line.fixMap(map, Collections.<String, Double>emptyMap());
assertThat(map, anEmptyMap());
Map<Line, Map<String, Double>> newMap = Line.fixMap(map, emptyMap());
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, Map<String, Double>> element : map.entrySet()) {
for (Map.Entry<Line, Map<String, Double>> element : map.entrySet()) {
assertNotNull(element.getKey());
Map<String, Double> value = element.getValue(); // force casting
assertNotNull(value);
assertSame(Collections.emptyMap(), element.getValue());
assertSame(emptyMap(), element.getValue());
}
}

@Test
public void testFixMapHashMap() {
HashMap<Line, HashMap<String, Double>> map = new HashMap<>();
HashMap<String, Double> empty = new HashMap<>();
assertEquals(0, map.size());
assertThat(map, anEmptyMap());
HashMap<Line, HashMap<String, Double>> newMap = Line.fixMap(map, empty);
assertSame(map, newMap);
assertEquals(Line.values().length, map.size());
for (Entry<Line, HashMap<String, Double>> element : map.entrySet()) {
for (Map.Entry<Line, HashMap<String, Double>> element : map.entrySet()) {
assertNotNull(element.getKey());
HashMap<String, Double> value = element.getValue(); // force casting
assertNotNull(value);
Expand Down
13 changes: 6 additions & 7 deletions common/test-helpers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ plugins {
}

dependencies {
api(libs.test.junit4) {
// we don't need this because we use org.hamcrest v2
exclude module: "hamcrest-core"
}
api(libs.test.hamcrest){
// this needs to be excluded because otherwise the hamcrest-core exclusion above doesn't work
exclude module: "junit"
api(libs.test.junit5)
runtimeOnly(libs.test.junit5.launcher)
api(libs.test.hamcrest) {
// Avoid leaking JUnit 4 into tests.
exclude group: "junit", module: "junit"
}
api(libs.test.mockito)
api(libs.test.mockito.junit5)
api(libs.test.gwen)
implementation(projects.common.logConsole)
}
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ micronaut-handlebars = "5.0.1"
snakeyaml = "2.2"

junit4 = "4.13.2"
junit5 = "5.10.1"
mockito = "5.8.0"
mockative = "2.0.1"
# Use this instead of 1.3
Expand Down Expand Up @@ -87,9 +88,13 @@ gms-places = { module = "com.google.android.libraries.places:places", version.re
gms-maps = { module = "com.google.android.gms:play-services-maps", version.ref = "google-maps" }

test-junit4 = { module = "junit:junit", version.ref = "junit4" }
test-junit5 = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit5" }
test-junit5-vintage = { module = "org.junit.vintage:junit-vintage-engine", version.ref = "junit5" }
test-junit5-launcher = { module = "org.junit.platform:junit-platform-launcher" }
test-mockative = { module = "io.mockative:mockative", version.ref = "mockative" }
test-mockative-processor = { module = "io.mockative:mockative-processor", version.ref = "mockative" }
test-mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
test-mockito-junit5 = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" }
test-hamcrest = { module = "org.hamcrest:hamcrest-junit", version.ref = "hamcrest" }
test-gwen = { module = "com.shazam:gwen", version.ref = "gwen" }
jtidy = { module = "com.github.jtidy:jtidy", version.ref = "jtidy" }
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pluginManager.withPlugin("org.gradle.java-base") {
}
}

pluginManager.withPlugin("org.gradle.java") {
testing.suites.named("test", org.gradle.api.plugins.jvm.JvmTestSuite).configure { useJUnitJupiter() }
}

pluginManager.withPlugin("com.android.base") {
android {
compileOptions {
Expand Down
6 changes: 4 additions & 2 deletions web/status-history/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ dependencies {
implementation(libs.slf4j.api)
runtimeOnly(libs.log4j.slf4j2)

testImplementation(projects.common.testHelpers)
testImplementation(projects.common.testHelpers) {
exclude(group: "org.slf4j", module: "slf4j-simple")
}
testImplementation(libs.micronaut.httpClient)
testImplementation(libs.jtidy)
}
Expand Down Expand Up @@ -70,7 +72,7 @@ application {
micronaut {
version = libs.versions.micronaut.asProvider().get()
runtime("jetty")
testRuntime("junit4")
testRuntime("junit5")
processing {
incremental(true)
}
Expand Down
1 change: 1 addition & 0 deletions web/status-history/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<!-- Tighten framework logs. -->
<Logger name="org.eclipse.jetty" level="INFO" />
<Logger name="io.micronaut" level="INFO" />
<Logger name="io.micronaut.http.client" level="DEBUG" />
<Logger name="io.netty" level="INFO" />
<Logger name="com.github.jknack.handlebars" level="INFO" />
<Logger name="reactor" level="INFO" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package net.twisterrob.travel.statushistory.controller

import io.micronaut.context.ApplicationContext
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import net.twisterrob.travel.statushistory.test.HtmlValidator.assertValidHtml
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.containsString
import org.junit.AfterClass
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertNotNull
import org.junit.BeforeClass
import org.junit.Test

// TODO use @MicronautTest in JUnit 5.
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test

/**
* @see IndexController
*/
@MicronautTest
class IndexControllerIntegrationTest {

@Inject
lateinit var client: BlockingHttpClient

@Test fun testIndex() {
val request: HttpRequest<Unit> = HttpRequest.GET("/")

val body = client.toBlocking().retrieve(request)
val body = client.retrieve(request)

assertNotNull(body)
assertThat(body, containsString("Better London Travel"))
Expand All @@ -29,35 +33,10 @@ class IndexControllerIntegrationTest {
@Test fun testFavicon() {
val request: HttpRequest<Unit> = HttpRequest.GET("/favicon.ico")

val body = client.toBlocking().retrieve(request, ByteArray::class.java)
val body = client.retrieve(request, ByteArray::class.java)

assertNotNull(body)
val expected = IndexController::class.java.getResourceAsStream("/public/favicon.ico")?.use { it.readBytes() }
assertArrayEquals(expected, body)
}

companion object {

private lateinit var server: EmbeddedServer
private lateinit var client: HttpClient

@JvmStatic
@BeforeClass fun setupServer() {
server = ApplicationContext
.run(EmbeddedServer::class.java)
client = server
.applicationContext
.createBean(HttpClient::class.java, server.url)
}

@JvmStatic
@AfterClass fun stopServer() {
if (this::server.isInitialized) {
server.stop()
}
if (this::client.isInitialized) {
client.stop()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.twisterrob.travel.statushistory.controller

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.test.annotation.MockBean
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import net.twisterrob.blt.data.StaticData
import net.twisterrob.blt.model.LineColors
import net.twisterrob.travel.domain.london.status.api.HistoryUseCase
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.endsWith
import org.hamcrest.Matchers.startsWith
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`

/**
* @see LineStatusHistoryController
*/
@MicronautTest
class LineStatusHistoryControllerIntegrationTest {

@Inject
lateinit var client: BlockingHttpClient

@MockBean(HistoryUseCase::class)
val historyUseCase: HistoryUseCase = mock()

@MockBean(StaticData::class)
val staticData: StaticData = mock()

private val lineColors: LineColors = mock()

@BeforeEach fun setUp() {
`when`(staticData.lineColors).thenReturn(lineColors)
}

@Test fun testEmptyRequest() {
val request: HttpRequest<Unit> = HttpRequest.GET("/LineStatusHistory")

val body = client.retrieve(request)

assertNotNull(body)
assertThat(body, startsWith("<!DOCTYPE html>\n<html lang=\"en\">"))
assertThat(body, endsWith("</html>\n"))
}
}
Loading

0 comments on commit fe71d01

Please sign in to comment.