Hi <@U0BUH9FRD>, I am playing around with Kofu fun...
# spring
g
Hi @sdeleuze, I am playing around with Kofu functional Bean DSL. I am using Spring-Data-JDBC with Spring-MVC and trying to autowire
NamedParameterJdbcTemplate
. However, I am have been receiving this error that no beans found for it while running tests. In a annotation based approach, we don’t have to supply an explicit
NamedParameterJdbcTemplate.
My sample app here: https://github.com/overfullstack/kofu-mvc-jdbc. And PFB some code snippets from it:
Copy code
val app = application(WebApplicationType.SERVLET) {
	beans {
		bean<SampleService>()
		bean<UserHandler>()
	}
	enable(dataConfig)
	enable(webConfig)
}
Copy code
val dataConfig = configuration {
    beans {
        bean<UserRepository>()
    }
    listener<ApplicationReadyEvent> {
        ref<UserRepository>().init()
    }
}

val webConfig = configuration {
	webMvc {
		port = if (profiles.contains("test")) 8181 else 8080
		router {
			val handler = ref<UserHandler>()
			GET("/", handler::hello)
			GET("/api", handler::json)
		}
		converters {
			string()
			jackson()
		}
	}
}
Copy code
class UserRepository(private val client: NamedParameterJdbcTemplate) {

	fun count() =
			client.queryForObject("SELECT COUNT(*) FROM users", emptyMap<String, String>(), Int::class.java)
}
Copy code
open class UserRepositoryTests {

    private val dataApp = application(WebApplicationType.NONE) {
        enable(dataConfig)
    }

    private lateinit var context: ConfigurableApplicationContext

    @BeforeAll
    fun beforeAll() {
        context = dataApp.run(profiles = "test")
    }

    @Test
    fun count() {
        val repository = context.getBean<UserRepository>()
        assertEquals(3, repository.count())
    }

    @AfterAll
    fun afterAll() {
        context.close()
    }
}