1

I have a ktor server and I'm trying to make unit tests. However I'm not able to find good documentation and I can't get it to work, I probably missed something but I am stuck now, it doesn't give any error but I don't think it's running the server.

import io.ktor.http.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.server.testing.*
import com.protecto.authorization.*
import com.typesafe.config.ConfigFactory
import io.ktor.client.*
import io.ktor.server.application.*
import io.ktor.server.config.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.junit.BeforeClass
import org.junit.Test
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.TestMethodOrder


@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class ApplicationTest {
    private lateinit var client: HttpClient

    companion object {
        val engine = TestApplicationEngine(createTestEnvironment {
            config = HoconApplicationConfig(ConfigFactory.load("application.conf"))
        })

        @BeforeClass
        @JvmStatic
        fun setup() {
            println("TESTING IS READY TO BEGIN!")
            engine.start(wait = false)
            engine.application.module()
        }
    }

    @BeforeEach
    fun init() {
        client = HttpClient()
    }

    @AfterEach
    fun cleanup() {
        client.close()
    }

    @Test
    fun testSomething() = with(engine) {
        with(handleRequest(HttpMethod.Get, "/")) {
            assertEquals(HttpStatusCode.OK, this.response.status())
        }
    }
}

I can only say that the println is not happening.

What I want is the server to be ran and then the tests to be executed. Here's what I have right now. The assert is null and fails and there is no valuable log:

u-ways
  • 6,136
  • 5
  • 31
  • 47

2 Answers2

0

You're mixing Junit4 and Junit5 annotations. The correct annotation for Junit5 is @BeforeAll:

import org.junit.jupiter.api.BeforeAll

class ApplicationTest {
    companion object {
        @BeforeAll
        @JvmStatic
        fun setup() {
            println("We have all mixed Junit4 and Junit5 annotations by accident in the past.")
        }
    }
}

The old (JUnit4) @BeforeClass and the new (JUnit5) @BeforeAll are similar in that they are both executed just once, before any of the tests in the class. So even if your class has 10 tests, the @BeforeAll method is executed just once.

For further details regarding the differences, see: stackoverflow.com/a/48137060/5037430

u-ways
  • 6,136
  • 5
  • 31
  • 47
0

I stumbled upon this while trying to solve a similar problem for @BeforeEach. It seems like the annotation had its name changed on ktor and it is now @BeforeTest: https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/-before-test/

delki8
  • 379
  • 4
  • 17