Skip to content

Commit

Permalink
port tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Sep 26, 2024
1 parent 1b2aa22 commit 934569e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ vaadin-core = { module = "com.vaadin:vaadin-core", version.ref = "vaadin" }
karibu-testing = "com.github.mvysny.kaributesting:karibu-testing-v24:2.1.8"
karibu-dsl = "com.github.mvysny.karibudsl:karibu-dsl-v23:2.1.4"
dynatest = "com.github.mvysny.dynatest:dynatest:0.25"
junit = "org.junit.jupiter:junit-jupiter-engine:5.11.0"
vaadinboot = "com.github.mvysny.vaadin-boot:vaadin-boot:13.1"
jakarta-servlet = "jakarta.servlet:jakarta.servlet-api:5.0.0"
h2 = "com.h2database:h2:2.2.224" # com.h2database:h2; https://repo1.maven.org/maven2/com/h2database/h2/
Expand Down
2 changes: 1 addition & 1 deletion vok-framework-vokdb/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dependencies {
api(project(":vok-framework"))
api(libs.vokorm)

testImplementation(libs.dynatest)
testImplementation(libs.junit)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation(libs.slf4j.simple)
testImplementation(libs.h2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package eu.vaadinonkotlin.vaadin.vokdb

import com.github.mvysny.dynatest.DynaTest
import com.github.mvysny.karibudsl.v10.bind
import com.github.mvysny.kaributesting.v10.MockVaadin
import com.github.vokorm.KEntity
import com.vaadin.flow.component.combobox.ComboBox
import com.vaadin.flow.data.binder.BeanValidationBinder
import org.junit.jupiter.api.Test
import kotlin.test.expect

class ConvertersTest : DynaTest({
usingH2Database()
beforeEach { MockVaadin.setup() }
afterEach { MockVaadin.tearDown() }

test("toId() test") {
class ConvertersTest : AbstractVaadinDbTest() {
@Test fun `toId() test`() {
data class Review(override var id: Long? = null, var person: Long? = null) : KEntity<Long>

val person = Person(personName = "foo")
Expand All @@ -34,4 +29,4 @@ class ConvertersTest : DynaTest({
binder.writeBean(r)
expect(person.id!!) { r.person }
}
})
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.vaadinonkotlin.vaadin.vokdb

import com.github.mvysny.dynatest.DynaNodeGroup
import com.github.mvysny.dynatest.DynaTestDsl
import com.github.mvysny.kaributesting.v10.MockVaadin
import com.github.vokorm.*
import com.gitlab.mvysny.jdbiorm.Dao
import com.gitlab.mvysny.jdbiorm.JdbiOrm
Expand All @@ -11,6 +10,10 @@ import com.zaxxer.hikari.HikariDataSource
import org.h2.Driver
import org.jdbi.v3.core.annotation.JdbiProperty
import org.jdbi.v3.core.mapper.reflect.ColumnName
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import java.time.LocalDate
import java.util.*

Expand Down Expand Up @@ -42,19 +45,21 @@ enum class MaritalStatus {
Widowed
}

@DynaTestDsl
fun DynaNodeGroup.usingH2Database() {
beforeGroup {
val config = HikariConfig().apply {
driverClassName = Driver::class.java.name // the org.h2.Driver class
jdbcUrl = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"
username = "sa"
password = ""
}
JdbiOrm.setDataSource(HikariDataSource(config))
db {
handle.createUpdate(
"""create table if not exists Test (
abstract class AbstractDbTest {
companion object {
@BeforeAll
@JvmStatic
fun setupDb() {
val config = HikariConfig().apply {
driverClassName = Driver::class.java.name // the org.h2.Driver class
jdbcUrl = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"
username = "sa"
password = ""
}
JdbiOrm.setDataSource(HikariDataSource(config))
db {
handle.createUpdate(
"""create table if not exists Test (
id bigint primary key auto_increment,
name varchar not null,
age integer not null,
Expand All @@ -63,13 +68,21 @@ fun DynaNodeGroup.usingH2Database() {
alive boolean,
maritalStatus varchar
)"""
).execute()
).execute()
}
}
}

afterGroup { JdbiOrm.destroy() }

@AfterAll
@JvmStatic
fun tearDownDb() {
JdbiOrm.destroy()
}
}
@BeforeEach @AfterEach
fun clearDb() = Person.deleteAll()
beforeEach { clearDb() }
afterEach { clearDb() }
}

abstract class AbstractVaadinDbTest : AbstractDbTest() {
@BeforeEach fun fakeVaadin() { MockVaadin.setup() }
@AfterEach fun tearDownVaadin() { MockVaadin.tearDown() }
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package eu.vaadinonkotlin.vaadin.vokdb

import com.github.mvysny.dynatest.DynaTest
import com.github.mvysny.karibudsl.v10.grid
import com.github.mvysny.kaributesting.v10.MockVaadin
import com.github.mvysny.kaributesting.v10._findAll
import com.vaadin.flow.component.UI
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.expect

class HasDataViewUtilsTest : DynaTest({
group("API test: populating components with data providers") {
usingH2Database()
beforeEach { MockVaadin.setup() }
afterEach { MockVaadin.tearDown() }

group("grid") {
test("entity data provider") {
class HasDataViewUtilsTest {
@Nested inner class `API test - populating components with data providers`() : AbstractVaadinDbTest() {
@Nested inner class GridTests {
@Test fun `entity data provider`() {
(0..10).forEach { Person(null, "foo $it", it).save() }
val cb = UI.getCurrent().grid<Person>(Person.dataProvider) {}
expect((0..10).map { "foo $it" }) { cb._findAll().map { it.personName } }
}
}
}
})
}

0 comments on commit 934569e

Please sign in to comment.