Skip to content

Commit

Permalink
replace DynaTest with JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Oct 24, 2024
1 parent 66b45c9 commit 0b79d46
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 19 deletions.
1 change: 1 addition & 0 deletions karibu-testing-v10/kt10-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dependencies {

// for testing purposes
api(libs.dynatest)
api(libs.junit.jupiterapi)
api(libs.slf4j.simple)
api(libs.karibudsl) {
exclude(module = "javax.el")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ import com.github.mvysny.kaributesting.v10.pro.confirmDialogTestbatch
import com.github.mvysny.kaributesting.v10.pro.gridProTestbatch
import com.github.mvysny.kaributesting.v10.pro.richTextEditorTests
import npmPolymerTemplateTestBatch
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import java.util.*

open class AbstractAllTests10(val isModuleTest: Boolean) {
@BeforeEach fun resetLocale() {
// make sure that Validator produces messages in English
Locale.setDefault(Locale.ENGLISH)
}

@Nested inner class RoutesTests : AbstractRoutesTests()
}

/**
* @param isModuleTest if true then this test run simulates a jar reusable component.
*/
Expand All @@ -18,9 +29,6 @@ fun DynaNodeGroup.allTests(isModuleTest: Boolean) {
Locale.setDefault(Locale.ENGLISH)
}

group("routes test") {
routesTestBatch()
}
group("basic utils") {
basicUtilsTestbatch()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.github.mvysny.kaributesting.v10

import com.github.mvysny.dynatest.DynaNodeGroup
import com.github.mvysny.dynatest.DynaTestDsl
import com.github.mvysny.dynatest.expectThrows
import com.github.mvysny.kaributesting.v10.mock.MockVaadinHelper
import com.github.mvysny.kaributools.VaadinVersion
import com.vaadin.flow.component.Component
import com.vaadin.flow.component.UI
import com.vaadin.flow.router.HasErrorParameter
import com.vaadin.flow.router.InternalServerError
import com.vaadin.flow.router.NotFoundException
import com.vaadin.flow.router.PreserveOnRefresh
import com.vaadin.flow.router.RouteNotFoundError
import com.vaadin.flow.server.VaadinContext
import com.vaadin.flow.server.VaadinSession
import com.vaadin.flow.server.startup.ApplicationRouteRegistry
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import test.app.MyRouteNotFoundError
import java.lang.Boolean
import kotlin.test.expect

val allViews: Set<Class<out Component>> = setOf<Class<out Component>>(
Expand All @@ -30,32 +25,31 @@ val allViews: Set<Class<out Component>> = setOf<Class<out Component>>(
)
val allErrorRoutes: Set<Class<out HasErrorParameter<*>>> = setOf(ErrorView::class.java, MockRouteNotFoundError::class.java, MockRouteAccessDeniedError::class.java)

@DynaTestDsl
fun DynaNodeGroup.routesTestBatch() {
afterEach { MockVaadin.tearDown() }
abstract class AbstractRoutesTests {
@AfterEach fun tearDownVaadin() { MockVaadin.tearDown() }

test("All views discovered") {
@Test fun `All views discovered`() {
val routes: Routes = Routes().autoDiscoverViews("com.github")
expect(allViews) { routes.routes.toSet() }
expect(allErrorRoutes) { routes.errorRoutes.toSet() }
}

test("calling autoDiscoverViews() multiple times won't fail") {
@Test fun `calling autoDiscoverViews() multiple times won't fail`() {
repeat(5) {
expect(allViews) { Routes().autoDiscoverViews("com.github").routes }
}
}

// https://github.com/mvysny/karibu-testing/issues/50
test("app-specific NotFoundException handler removes MockRouteNotFoundError") {
@Test fun `app-specific NotFoundException handler removes MockRouteNotFoundError`() {
val routes: Routes = Routes().autoDiscoverViews()
val expectedRouteClasses = setOf(ErrorView::class.java, InternalServerError::class.java, MyRouteNotFoundError::class.java, MockRouteAccessDeniedError::class.java)
expect(expectedRouteClasses) { routes.errorRoutes.toSet() }
// make sure that Vaadin initializes properly with this set of views
MockVaadin.setup(routes)
}

test("PWA is ignored by default") {
@Test fun `PWA is ignored by default`() {
val routes: Routes = Routes().autoDiscoverViews("com.github")
val ctx: VaadinContext = MockVaadinHelper.createMockVaadinContext()
routes.register(ctx)
Expand All @@ -65,7 +59,7 @@ fun DynaNodeGroup.routesTestBatch() {
}
}

test("PWA is discovered properly if need be") {
@Test fun `PWA is discovered properly if need be`() {
val routes: Routes = Routes().autoDiscoverViews("com.github")
routes.skipPwaInit = false
val ctx: VaadinContext = MockVaadinHelper.createMockVaadinContext()
Expand All @@ -76,7 +70,7 @@ fun DynaNodeGroup.routesTestBatch() {
}
}

test("MockRouteNotFoundError is called when the route doesn't exist, and it fails immediately with an informative error message") {
@Test fun `MockRouteNotFoundError is called when the route doesn't exist, and it fails immediately with an informative error message`() {
val routes: Routes = Routes().autoDiscoverViews("com.github")
MockVaadin.setup(routes)
expectThrows(NotFoundException::class, "No route found for 'A_VIEW_THAT_DOESNT_EXIST'\nAvailable routes:") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.mvysny.kaributesting.v10

import com.github.mvysny.dynatest.DynaNodeGroup
import com.github.mvysny.dynatest.DynaTestDsl
import org.junit.jupiter.api.Nested

/**
* @param isModuleTest if true then this test run simulates a jar reusable component.
Expand Down Expand Up @@ -42,3 +43,7 @@ fun DynaNodeGroup.allTests19(isModuleTest: Boolean) {
tabsTests()
}
}

abstract class AbstractAllTests19(val isModuleTest: Boolean) {
@Nested inner class AllTests10 : AbstractAllTests10(isModuleTest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import com.github.mvysny.dynatest.DynaTest
import com.github.mvysny.dynatest.jvmVersion
import com.github.mvysny.kaributesting.v10.*
import com.github.mvysny.kaributools.VaadinVersion
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URL
import kotlin.test.expect

Expand All @@ -22,3 +26,22 @@ class AllTests : DynaTest({
}
allTests19(isModuleTest = true)
})

class AllTests24 {
@Test fun `flow-build-info-json doesn't exist`() {
val res: URL? = Thread.currentThread().contextClassLoader.getResource("META-INF/VAADIN/config/flow-build-info.json")
expect(null, "flow-build-info.json exists on the classpath!") { res }
}

@Nested inner class VaadinEnv {
@BeforeEach fun fakeVaadin() { MockVaadin.setup() }
@AfterEach fun tearDownVaadin() { MockVaadin.tearDown() }

@Test fun vaadinVersion() {
expect(24) { VaadinVersion.get.major }
expect(false) { VaadinMeta.isCompatibilityMode }
}
}

@Nested inner class AllTests19 : AbstractAllTests19(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import com.github.mvysny.dynatest.DynaTest
import com.github.mvysny.dynatest.jvmVersion
import com.github.mvysny.kaributesting.v10.*
import com.github.mvysny.kaributools.VaadinVersion
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URL
import kotlin.test.expect

Expand All @@ -22,3 +26,22 @@ class AllTests : DynaTest({
}
allTests19(isModuleTest = false)
})

class AllTests24 {
@Test fun `flow-build-info-json exists`() {
val res: URL? = Thread.currentThread().contextClassLoader.getResource("META-INF/VAADIN/config/flow-build-info.json")
expect(true, "flow-build-info.json is not on the classpath!") { res != null }
}

@Nested inner class VaadinEnv {
@BeforeEach fun fakeVaadin() { MockVaadin.setup() }
@AfterEach fun tearDownVaadin() { MockVaadin.tearDown() }

@Test fun vaadinVersion() {
expect(24) { VaadinVersion.get.major }
expect(false) { VaadinMeta.isCompatibilityMode }
}
}

@Nested inner class AllTests19 : AbstractAllTests19(false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import com.github.mvysny.dynatest.DynaTest
import com.github.mvysny.dynatest.jvmVersion
import com.github.mvysny.kaributesting.v10.*
import com.github.mvysny.kaributools.VaadinVersion
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URL
import kotlin.test.expect

Expand All @@ -22,3 +26,22 @@ class AllTests : DynaTest({
}
allTests19(isModuleTest = true)
})

class AllTests24 {
@Test fun `flow-build-info-json doesn't exist`() {
val res: URL? = Thread.currentThread().contextClassLoader.getResource("META-INF/VAADIN/config/flow-build-info.json")
expect(null, "flow-build-info.json exists on the classpath!") { res }
}

@Nested inner class VaadinEnv {
@BeforeEach fun fakeVaadin() { MockVaadin.setup() }
@AfterEach fun tearDownVaadin() { MockVaadin.tearDown() }

@Test fun vaadinVersion() {
expect(24) { VaadinVersion.get.major }
expect(false) { VaadinMeta.isCompatibilityMode }
}
}

@Nested inner class AllTests19 : AbstractAllTests19(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import com.github.mvysny.dynatest.DynaTest
import com.github.mvysny.dynatest.jvmVersion
import com.github.mvysny.kaributesting.v10.*
import com.github.mvysny.kaributools.VaadinVersion
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URL
import kotlin.test.expect

Expand All @@ -22,3 +26,22 @@ class AllTests : DynaTest({
}
allTests19(isModuleTest = false)
})

class AllTests24 {
@Test fun `flow-build-info-json exists`() {
val res: URL? = Thread.currentThread().contextClassLoader.getResource("META-INF/VAADIN/config/flow-build-info.json")
expect(true, "flow-build-info.json is not on the classpath!") { res != null }
}

@Nested inner class VaadinEnv {
@BeforeEach fun fakeVaadin() { MockVaadin.setup() }
@AfterEach fun tearDownVaadin() { MockVaadin.tearDown() }

@Test fun vaadinVersion() {
expect(24) { VaadinVersion.get.major }
expect(false) { VaadinMeta.isCompatibilityMode }
}
}

@Nested inner class AllTests19 : AbstractAllTests19(false)
}

0 comments on commit 0b79d46

Please sign in to comment.