Skip to content

Commit

Permalink
works on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sombriks committed Feb 16, 2024
1 parent 0693626 commit ecbe801
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 28 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ java -jar build/libs/sample-htmx-javalin-1.0-SNAPSHOT-all.jar
- [Shadow plugin][20] does similar job done by [maven shade plugin][40]
- Javalin supports a good range of [template engines][50], just pick one
- There are a good amount of [logging options][60] to Javalin
- [Testing Javalin][01] is simple and ergonomic thanks to its testing library
- [Spock][90] demands us to enable the groovy language in the project
- Groovy support has issues to proper set jvm toolchain, fallbacks to system
- There is a [nice htmx plugin][70] for intellij
Expand All @@ -49,10 +50,10 @@ java -jar build/libs/sample-htmx-javalin-1.0-SNAPSHOT-all.jar

## Next steps

- A good form to object mapper would be handy. Javalin has [validators][90] but
besides that, form to model mapping is pretty much manual
- Need a coverage solution that handles spock properly
-
- [ ] Find a good form to object mapper would be handy. Javalin has
[validators][90] but besides that, form to model mapping is pretty much manual
- [ ] Need a coverage solution that handles spock properly
- [ ] Setup docker image building and registry publishing

[00]: https://github.com/sombriks/sample-htmx-javalin
[10]: https://www.jetbrains.com/idea/download
Expand All @@ -64,3 +65,4 @@ java -jar build/libs/sample-htmx-javalin-1.0-SNAPSHOT-all.jar
[70]: https://plugins.jetbrains.com/plugin/20588-htmx-support
[80]: https://jdbi.org/releases/3.44.1/#_fluent_api
[90]: https://spockframework.org/spock/docs/2.3/spock_primer.html
[01]: https://javalin.io/tutorials/testing#functionalintegration-tests
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation("org.slf4j:slf4j-simple:2.0.10")
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")

testImplementation("io.javalin:javalin-testtools:6.0.1")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.spockframework:spock-core:2.3-groovy-4.0")

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/sample/htmx/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class App(
private val logger by lazy { LoggerFactory.getLogger(App::class.java) }

fun start(port: Int = 8080) {
logger.info("start app!")
logger.info("start app on port $port")
javalin.start(port)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/sample/htmx/config/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Database {
Jdbi.create(dataSource)
}

@JvmStatic
fun init() {
jdbi.withHandle<Any, Exception> { handle ->
handle.execute("""
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/sample/htmx/service/TodoService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TodoService(val db: Jdbi = Database.jdbi) {
"""
insert into todos(description, done)
values (:description, :done)
returning id
""".trimIndent()
).bindBean(item).execute()
}
Expand Down
20 changes: 0 additions & 20 deletions src/test/groovy/AppTest.groovy

This file was deleted.

26 changes: 26 additions & 0 deletions src/test/groovy/sample/htmx/AppTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sample.htmx

import io.github.cdimascio.dotenv.Dotenv
import spock.lang.Specification

class AppTest extends Specification {

def app = new App()
def dotenv = Dotenv.configure().load()

def "Should check app got dependencies properly injected"() {
expect:
app.javalin != null
app.controller != null
app.controller.service != null
app.controller.service.db != null
}

def "should check env is test"() {
when:
def mode = dotenv.get("MODE")

then:
mode == "test"
}
}
21 changes: 21 additions & 0 deletions src/test/groovy/sample/htmx/ServiceTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sample.htmx

import sample.htmx.config.Database
import sample.htmx.service.TodoService
import spock.lang.Shared
import spock.lang.Specification

class ServiceTest extends Specification {

@Shared
def service = new TodoService()

def setup() {
Database.init()
}

def "Should list todos"() {
expect:
service.list() != null
}
}
24 changes: 24 additions & 0 deletions src/test/kotlin/sample/htmx/ApiTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sample.htmx

import io.javalin.testtools.JavalinTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import sample.htmx.config.Database

class ApiTest {

private val app = App()

@BeforeEach
fun setup(){
Database.init()
}

@Test
fun `Should check endpoints`() = JavalinTest.test(app.javalin) { server, client ->
// basic GET endpoints
Assertions.assertEquals(200, client.get("/").code)
Assertions.assertEquals(200, client.get("/todos").code)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package sample.htmx

import io.github.cdimascio.dotenv.Dotenv
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import sample.htmx.App
import sample.htmx.config.Database
import sample.htmx.service.TodoService

class SimpleTest {

Expand Down

0 comments on commit ecbe801

Please sign in to comment.