Mati Galli
08/01/2020, 7:46 PMclass OptionGroupControllerTest : KoinTest {
private val optionGroup: String = this::class.java
.getResource("/jsons/option_group.json")
.readText()
.replace("\n", "")
.replace(" ", "")
@BeforeTest
fun setup() {
startKoin {
module {
single<OptionGroupController>()
single<OptionGroupService>()
}
}
}
@AfterTest
fun teardown() {
stopKoin()
}
@Test
fun `create option`() = withTestApplication(Application::routes) {
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/optionGroup") {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(optionGroup)
}) {
assertEquals(HttpStatusCode.Created, response.status())
}
}
}
My Routes:
fun Application.routes() {
val controllerOptionGroup: OptionGroupController by inject()
val controllerOption: OptionController by inject()
routing {
get("/") {
call.respondText("OK")
}
get("/health") {
call.respondText("OK")
}
route("/optionGroup") {
post {
try {
val message = call.receive<String>()
val messageOptionGroup: OptionGroupDTO = Mapper.read(message)
println("SERVER: Message from the client: $message")
call.respond(controllerOptionGroup.create(call, messageOptionGroup))
} catch (ex: ItemException) {
call.respond(HttpStatusCode.BadRequest, mapOf("code" to ex.code, "messages" to arrayListOf(ex.message)))
}
}
}
}
}
And the exception is thrown at line:
call.respond(controllerOptionGroup.create(call, messageOptionGroup))
This problem only gives me when running the tests. When I run the application everything works correctly.
I would really appreciate if someone can give me a hand with this 🙂Nikky
08/01/2020, 8:55 PMinstall(Koin) {}
block?Mati Galli
08/02/2020, 6:12 PMMati Galli
08/02/2020, 9:08 PMclass OptionGroupControllerTest : KoinTest {
private val optionGroup: String = this::class.java
.getResource("/jsons/option_group.json")
.readText()
.replace("\n", "")
.replace(" ", "")
private val testModule: Module = module(override = true) {
single { OptionGroupService() }
single { OptionGroupController(get()) }
}
fun Application.koinInitialization() {
install(Koin) {
module { testModule }
}
}
@BeforeTest
fun setup(){
}
@AfterTest
fun teardown() {
stopKoin()
}
@Test
fun `create option`() = withTestApplication(Application::routes) {
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/optionGroup") {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(optionGroup)
}) {
assertEquals(HttpStatusCode.Created, response.status())
}
}
}
Mati Galli
08/03/2020, 12:36 PMstartKoin {
module {
single<OptionGroupController>()
single<OptionGroupService>()
}
}
And now I do this and is working:
private val optionGroupModules: Module = module {
single { Conf }
single { OptionGroupService() }
single { OptionGroupController(get()) }
}
startKoin { modules(optionGroupModules) }
Nikky
08/03/2020, 2:52 PMwithTestApplication
call
if you write it like so
withTestApplication(
{
install(Koin) {
modules(optionGroupModules)
}
routes()
}
) {
// test code here
}
it should work
and this can be refactored into a utility function so you have the same setup of the test application each timeMati Galli
08/03/2020, 4:22 PMMati Galli
08/03/2020, 4:39 PMfun testFeaturesSetup(modules: Module): Application.() -> Unit {
return {
install(Koin) {
modules(modules)
}
routes()
}
}
In the test file
private val optionModules: Module = module {
single { Conf }
single { OptionGroupService() }
single { OptionService(get()) }
single { OptionController(get()) }
single { OptionGroupController(get()) }
}
@Test
private fun `create option`() = withTestApplication(testFeaturesSetup(optionModules)) {
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/optionGroup") {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(optionGroup)
}) {
assertEquals(HttpStatusCode.OK, response.status())
}
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/option") {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(option)
}) {
assertEquals(HttpStatusCode.OK, response.status())
}
}
A lot better 🙂