Spring Autowire not initialized with KotlinTest 3.1.8 - spring

I know this should be possible, as others have reported it working. However, in a simple 'demo' project (Spring Initializer), I can't get the autowired property to be initialized within the test.
When I execute the test in "JpaTestApplicationTests", I can't get past receiving "lateinit property repo has not been initialized
kotlin.UninitializedPropertyAccessException".
Can anyone help me understand what I'm doing wrong?
Below is my application file, bean to be autowired, and test:
Application:
package com.example.jpaTest
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
#SpringBootApplication
class JpaTestApplication
fun main(args: Array<String>) {
SpringApplication.run(JpaTestApplication::class.java, *args)
}
Bean:
package com.example.jpaTest
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
#Repository
interface FakeRepository : CrudRepository<Fake, String>
Test:
package com.example.jpaTest
import io.kotlintest.*
import io.kotlintest.specs.*
import io.kotlintest.spring.SpringListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
#ContextConfiguration(classes = [JpaTestApplication::class])
class JpaTestApplicationTests : StringSpec() {
override fun listeners() = listOf(SpringListener)
#Autowired
lateinit var repo: FakeRepository
init {
"can get by id"{
val it = Fake("Blah","testName1")
val saved = repo.save(it)
repo.findOne(saved.uuid) shouldBe saved
}
}
}
Relevant Gradle entries:
buildscript {
ext {
kotlinVersion = '1.2.61'
springBootVersion = '1.5.16.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: "kotlin-jpa"
apply plugin: 'org.springframework.boot'
test {
useJUnitPlatform()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('com.fasterxml.jackson.module:jackson-module-kotlin')
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.8'
testCompile 'io.kotlintest:kotlintest-extensions-spring:3.1.8'
}

Related

MapStruct not injected in Kotlin project

I am trying to create a project with current Kotlin, MapStruct and Java using Spring-Boot folowing some online examples, as I am new to MapStruct, however I am not able to inject the mapper into my service. Both Idea and Gradle (in build task test) complain that no bean has been found (UnsatisfiedDependencyException). Googling didn't help. What am I missing?
MWE:
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.7.4"
id("io.spring.dependency-management") version "1.0.14.RELEASE"
kotlin("jvm") version "1.7.20"
kotlin("plugin.spring") version "1.7.20"
kotlin("plugin.jpa") version "1.7.20"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation("org.mapstruct:mapstruct:1.5.3.Final")
annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
DemoAppllication.kt
package com.example.demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
App.kt
package com.example.demo
import org.mapstruct.Mapper
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import javax.persistence.*
#Entity
#Table(name = "items")
class Item(#Id var id: Int = 0)
data class ItemDto(val id: Int)
#Repository
interface ItemRepo : JpaRepository<Item, Int>
#Mapper(componentModel = "spring")
interface ItemMapper {
fun entToDto(item: Item) : ItemDto
fun entsToDtos(items: List<Item>) : List<ItemDto>
fun dtoToEnt(itemDto: ItemDto) : Item
}
#Service
class Srvc(private val itemMapper: ItemMapper, // XXX: no bean found for this one
private val repo: ItemRepo)
{
fun items() = itemMapper.entsToDtos(repo.findAll())
}
// controller skipped
Kapt is in maintenance mode! See example: https://github.com/mapstruct/mapstruct-examples/tree/main/mapstruct-kotlin
plugin {
// ...
kotlin("kapt")
// ...
}
dependencies {
// ...
kapt("org.mapstruct:mapstruct-processor:$version")
implementation("org.mapstruct:mapstruct:$version")
// ...
}
EDIT (by mpts.cz — for future me or anyone as new to Mapstruct and/or Gradle as I am):
use plugin kotlin("kapt")
replace annotationProcessor("org.mapstruct:mapstruct-processor:$version")
with kapt("org.mapstruct:mapstruct-processor:$version")
put the previous line before implementation("org.mapstruct:mapstruct:$version")
With these changes all seems to work smoothly. Thanks Numichi!

Spring Boot Kotlin Cannot Find Repository Beans

I'm just getting started with Spring Boot + Kotlin and I was trying out the PagingAndSortingRepository interface for JPA so I wrote the following interface:
interface CustomerRepository : PagingAndSortingRepository<Customer, Long>
The model for Customer is below:
#Entity
data class Customer(
#Id #GeneratedValue var id: Long,
var name: String
)
Now I'm trying to hook it up with a CustomerService which looks like this:
#Service
class CustomerService(
private val customerRepository: CustomerRepository
) {
fun getAllCustomers(): Collection<Customer> = customerRepository.findAll().toList()
fun addCustomer(customer: Customer) = customerRepository.save(customer)
fun deleteCustomer(customer: Customer) = customerRepository.delete(customer)
fun updateCustomer(customer: Customer) = customerRepository.save(customer)
}
And the Application looks like this:
#SpringBootApplication
#Configuration
#EnableAutoConfiguration
#EnableJpaRepositories
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
I've added the required dependencies I believe, which are shown below:
plugins {
id("org.springframework.boot") version "2.5.0-SNAPSHOT"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.4.30"
kotlin("plugin.spring") version "1.4.30"
}
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.apache.derby:derby:10.15.2.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
Spring Boot is not able to find a bean which sort of makes sense as I haven't defined one. However reading the documentation, it looks like one should be generated by Spring Boot here: Spring Boot Data Repositories
Application.properties is
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
The error message I get is:
Description:
Parameter 0 of constructor in com.ubiquifydigital.crm.service.CustomerService required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
I saw a few different posts regarding this and have tried adding the Configuration, AutoConfiguration and EnableJpaRepositories annotations however that has only changed the error to entityManagerFactory not found instead of the CustomerRepository not found.
When using default in-memory db you must define
spring.jpa.hibernate.ddl-auto=update
in application.properties as informed here. You are also missing #Autowired annotation. entityManagerFactory is missing because the default auo configuration is turned off, in that case application is expecting you to do all the necessary configuration which again you are not doing. So keep the default configuration on and change what you need.
This code is assumed in a single file.
If you are having multiple packages then you may need to add as mentioned in this link
Working code:
package com.example.demo
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import lombok.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.Table
#SpringBootApplication
open class SpringBootDerbyAppApplication
fun main(args: Array<String>) {
runApplication<SpringBootDerbyAppApplication>(*args)
}
#Entity
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "applog")
internal class AppLog {
#Id
#GeneratedValue
private val id: Long = 0
#JsonProperty
private val name: String? = null
}
#Configuration
open class ObjectMapperConfiguration {
#Bean
#Primary
open fun objectMapper() = ObjectMapper().apply {
registerModule(KotlinModule())
}
}
#RestController
#RequestMapping(path = ["/logs"])
internal class LogController #Autowired constructor(private val appLogRepository: AppLogRepository) {
#GetMapping(path = ["/"])
fun logs(): MutableIterable<AppLog> {
return appLogRepository.findAll()
}
#PostMapping(path = ["/"])
fun add(#RequestBody appLog: AppLog): AppLog {
appLogRepository.save(appLog)
return appLog
}
}
#Repository
internal interface AppLogRepository : CrudRepository<AppLog, Long>
gradle file
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.5.0-M1'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.apache.derby:derby'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

How to configure spock in spring boot with gradle 6+ and java 11+

I want to use spock in my spring-boot project (using groovy, not java). I have some projects with spock already working in java 8 and lower versions of gradle but I can't find documentation about the newest versions.
This is my OLD build.gradle
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.codehaus.groovy:groovy')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testCompile(
'junit:junit:4.12',
'org.codehaus.groovy:groovy-all:2.4.4',
'org.spockframework:spock-core:1.2-groovy-2.4',
'cglib:cglib:2.2',
'org.spockframework:spock-spring:1.2-groovy-2.4',
)
}
this works and I can create spock tests but when I create a project with java 11 and gradle 6.4, nothing works anymore (the gradle syntax is way different and the spock libraries don't work anymore),
this is my CURRENT (and failing) build.gradle:
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'groovy'
}
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.codehaus.groovy:groovy'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.postgresql:postgresql'
testImplementation("org.spockframework:spock-core") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}
testImplementation group: 'org.spockframework', name: 'spock-spring', version: '2.0-M3-groovy-3.0'
testImplementation(enforcedPlatform("org.spockframework:spock-bom:2.0-M1-groovy-2.5"))
}
test {
useJUnitPlatform()
}
With this, nothing works but the gradle build doesn't found the spock-spring libraries.
How can I apply the spock libraries to spring boot with the newest versions of java and gradle?
Think you got into a muddle of Groovy Versions with the dependencies:
I tested with this build:
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'groovy'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.codehaus.groovy:groovy:3.0.5'
testImplementation('org.spockframework:spock-core:2.0-M3-groovy-3.0')
testImplementation('org.spockframework:spock-spring:2.0-M3-groovy-3.0')
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
And this test ran and passed:
package test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ApplicationContext
import spock.lang.Specification
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Application)
class ContextSpec extends Specification {
#Autowired
ApplicationContext context
def "context is as expected"() {
expect:
context
context.getBean("echoService")
}
}
(Obviously, I had an echoService in my context)
For completeness, here's the service:
package test
import org.springframework.stereotype.Service
#Service("echoService")
class EchoService {
String echo(String value) {
value?.reverse()
}
}
The controller:
package test
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
#RestController
class EchoController {
final EchoService echoService
EchoController(EchoService echoService) {
this.echoService = echoService
}
#RequestMapping(
value = "/echo/{message}",
method = RequestMethod.GET,
produces = MediaType.TEXT_PLAIN_VALUE
)
String doEcho(#PathVariable String message) {
return echoService.echo(message)
}
}
And the application class:
package test
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
#SpringBootApplication
class Application {
static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And proof 😉
➜ curl localhost:8080/echo/hello
olleh%

Why cant i import #RestController spring boot

enter image description herei'm just trying to set up a simple spring boot application that has rest controller. But cant import Rest Controller. Here is my main method
package com.test.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
#RestController
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
And here is my build.gradle. the script collects all the jars on the classpath and builds a single jar.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-web'
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Any Ideas? Thanks
You are missing import for #RequestMapping("/").
import org.springframework.web.bind.annotation.RequestMapping;
I invalidated and restarted the caches which worked!

How to dependency-inject CqlSession?

In order to use ShedLock with Cassandra, I need the CqlSession to create a CassandraLockProvider, according to the docs.
However, I always get the following error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.datastax.oss.driver.api.core.CqlSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
This full minimal example below reproduces the issue:
src/main/kotlin/com/foo/cqlsessioninjection/application.kt
package com.foo.cqlsessioninjection
import com.datastax.oss.driver.api.core.CqlSession
import net.javacrumbs.shedlock.core.LockProvider
import net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
#SpringBootApplication
#EnableScheduling
#EnableSchedulerLock(defaultLockAtMostFor = "PT23H")
class CqlSessionInjection
#Configuration
class SchedulerConfiguration {
#Bean
fun lockProvider(cqlSession: CqlSession): LockProvider {
return CassandraLockProvider(cqlSession)
}
}
#Component
class SomeTask {
#Scheduled(cron = "*/1 * * * * ?") // every second
#SchedulerLock(name = "some_task")
fun runSomeTask() {
println("some task")
}
}
fun main(args: Array<String>) {
runApplication<CqlSessionInjection>(*args)
}
build.gradle
buildscript {
ext {
kotlinVersion = '1.3.71'
springBootVersion = '2.2.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.foo'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
repositories {
mavenCentral()
}
dependencies {
implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-spring', version: '4.9.1'
implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-provider-cassandra', version: '4.9.1'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-cassandra'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8'
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect'
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.3'
compile group: 'com.google.guava', name: 'guava', version: '23.3-jre'
testCompile group: 'org.cassandraunit', name: 'cassandra-unit-spring', version: '3.11.2.0'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
}
task downloadDependencies(type: Exec) {
configurations.testRuntime.files
commandLine 'echo', 'Downloaded all dependencies'
}
compileKotlin.dependsOn(processResources)
compileJava.dependsOn(processResources)
settings.gradle
rootProject.name = 'CqlSessionInejction'
src/test/kotlin/com/foo/cqlsessioninjection/integration/ContextTest.kt
package com.foo.cqlsessioninjection.integration
import org.cassandraunit.spring.CassandraDataSet
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener
import org.cassandraunit.spring.EmbeddedCassandra
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestExecutionListeners
import org.springframework.test.context.junit4.SpringRunner
#RunWith(SpringRunner::class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#TestExecutionListeners(
listeners = [CassandraUnitDependencyInjectionTestExecutionListener::class],
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
#CassandraDataSet(value = ["cql/cql_session_injection.cql"], keyspace = "shedlock")
#EmbeddedCassandra
class ContextTest {
#Test
fun `test task`() {
println("starting to sleep")
Thread.sleep(4000)
println("done sleeping")
}
}
src/test/resources/application.yml
spring:
data:
cassandra:
contact-points: localhost
port: 9142
keyspace_name: shedlock
src/test/resources/cql/cql_session_injection.cql
DROP KEYSPACE IF EXISTS shedlock;
CREATE KEYSPACE shedlock
WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};
CREATE TABLE shedlock.lock (
name text PRIMARY KEY,
lockUntil timestamp,
lockedAt timestamp,
lockedBy text
);
The doc you've referenced points out that a CqlSession bean has to be created but it does not really explain how you get there. I would report that as a documentation issue to this project to make it a bit more explicit.
You need Spring Boot 2.3 to get Cassandra v4 support. The version you are using (2.2x) uses Cassandra v3 and a completely different API.

Resources