Could not resolve 'spring-boot-security-test' when testing service with mockk - spring-boot

I'm currently getting the following error when I try to test my service:
Execution failed for task ':compileTestKotlin'.
> Error while evaluating property 'filteredArgumentsMap' of task ':compileTestKotlin'.
> Could not resolve all files for configuration ':testCompileClasspath'.
> Could not find org.springframework.boot:spring-boot-security-test:.
Required by:
project :
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
Currently, my test file looks like this:
class UserServiceTest {
val userRepository: HireOutUserRepository = mockk()
val passwordEncoder: BCryptPasswordEncoder = BCryptPasswordEncoder()
val userService: UserService = UserServiceImpl(userRepository, passwordEncoder)
#Test
fun whenGetAllUsers_thenReturnSize() {
verify(exactly = 0) { userService.getUsers() }
}
}
and my build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.1"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
}
group = "com.jre"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
//implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
//testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-security-test")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "org.mockito", module = "mockito-core")
}
testImplementation("org.junit.jupiter:junit-jupiter-engine")
testImplementation("io.mockk:mockk:1.13.2")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I'm not sure what is going on to cause the error to trigger as this should be a simple test scenario in my code.
Thanks

Dependency is incorrect. The correct dependency is this.
testImplementation 'org.springframework.security:spring-security-test'
Please add this in build.gradle file and try again

Related

Test EntityManager with Junit5 Kotlin

I am trying to introduce to Spring JPA and I have difficulties running up my tests.
My gradle.build.kts looks like the following
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.4.0"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.4.10"
kotlin("plugin.spring") version "1.4.10"
kotlin("plugin.jpa") version "1.4.10"
}
group = "com.pluralsight"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
jcenter()
google()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
testImplementation(platform("org.junit:junit-bom:5.7.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I am using Junit5 for my test framework. And what first I need to test is that my Flight Entity is created correctly. I am not using #SpringRunner since we are on Junit5 so I do the following:
package com.pluralsight.springdataoverview
import com.pluralsight.springdataoverview.entity.Flight
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.context.SpringBootTest
import java.time.LocalDateTime
import javax.persistence.EntityManager
#SpringBootTest
#DataJpaTest
class SpringDataOverviewApplicationTests {
#Autowired
private val entityManager: EntityManager? = null
#Test
fun verifyFlighTCanBeSaved() {
var flight = Flight()
flight.origin = "London"
flight.destination = "New York"
flight.scheduledAt = LocalDateTime.parse("2011-12-13T12:12:00")
entityManager!!.persist(flight)
val flights = entityManager
.createQuery("SELECT f FROM Flight f", Flight::class.java)
.resultList
Assertions.assertEquals(flights.first(), flight)
}
}
And I have the following in red
What dependency I am missing ?
You have multiple declarations of configuration (that's what your error message is saying).
It's because you are using #SpringBootTest and #DataJpaTest in the same configuration class i.e SpringDataOverviewApplicationTests.
Use either of it and it should be fine.

Pass array list of topic names to #KafkaListener

I am trying to follow https://github.com/spring-projects/spring-kafka/issues/361 to pass topic names from .yml file to the #kafkalistener. But the compiler throws following error
Type mismatch.
Required:
Array<String>
Found:
String
Unresolved reference: spring
Below is the receiver code
#Component
class Receiver {
companion object {
private val LOGGER = LoggerFactory.getLogger(Receiver::class.java)
}
#Autowired
private val taskExecutor: TaskExecutor? = null
#Autowired
private val applicationContext: ApplicationContext? = null
#KafkaListener(topics = "#{'${spring.kafka.topics}'.split(',')}")
fun receive(#Header(KafkaHeaders.RECEIVED_TOPIC) topic: String) {
}
}
Below is my build.gradle file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.7.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.kafka:spring-kafka")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
What am i missing here?
This works for me:
#KafkaListener(topics = ["#{'\${test.topics}'.split(',')}"])
Pay attention how I had to apply Kotlin syntax to the annotation attribute value.
Also Keep in mind that $ is a template specific operator in Kotlin, so we need to escape it to make it as a plain symbol for further properties placeholder resolution.

Unresolved reference: compileKotlin in build.gradle.kts

Kotlin project success build by build.gradle:
compileKotlin {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}
compileTestKotlin {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}
Nice.
But I need to change to build.gradle.kts:
plugins {
kotlin("jvm") version "1.2.10"
id("application")
}
group = "com.myproject"
version = "1.0-SNAPSHOT"
application {
mainClassName = "MainKt"
}
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
jcenter()
}
val kotlinVer = "1.2.10"
dependencies {
compile(kotlin(module = "stdlib-jre8", version = kotlinVer))
implementation("com.google.code.gson:gson:2.7")
implementation("com.squareup.okhttp3:logging-interceptor:3.8.0")
implementation("com.squareup.retrofit2:converter-gson:2.1.0")
implementation("com.squareup.retrofit2:retrofit:2.5.0")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
and now I get error:
Line 32: compileKotlin {
^ Unresolved reference: compileKotlin
There's an issue in the Kotlin Gradle DSL that causes this.
https://github.com/gradle/kotlin-dsl-samples/issues/1368
You will need to use the following workaround until it gets resolved.
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
Following official Kotlin documentation (using Gradle part), I suggest to use such constructions in the build.gradle.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version ("1.3.21")
}
// repositories, dependencies, etc...
val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
Use withType keyword:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlin = "1.3.61"
kotlin("jvm") version kotlin apply false
}
subprojects {
repositories { mavenCentral(); mavenLocal() }
apply(plugin = "org.jetbrains.kotlin.jvm")
tasks {
val java: String by project
withType<KotlinCompile>{
kotlinOptions { jvmTarget = java }; sourceCompatibility = java; targetCompatibility = java
}
}
}

Gradle, Kotlin, Multi Module Project: Getting build errors while compiling this project

I am getting the following error on running gradle build command on root folder.
E:\Code\mdh>gradle build
> Task :multidemo:compileKotlin FAILED
e: E:\Code\mdh\multidemo\src\main\kotlin\com\simbalarry\multidemo\SampleController.kt: (3, 23): Unresolved reference: somelib
e: E:\Code\mdh\multidemo\src\main\kotlin\com\simbalarry\multidemo\SampleController.kt: (18, 12): Unresolved reference: Tester
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':multidemo:compileKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 5s
4 actionable tasks: 1 executed, 3 up-to-date
Below is my project structure and multidemo project is using some functionalities of somelib. Also, I have deliberetly kept somelib as spring boot project as I will be adding more spring boot related functionalities later.
Root Project
|
|---settings.gradle
|---somelib
| |
| |---build.gradle
| |---settings.gradle
| |---src/main/kotlin/somepackage/Tester.kt
| |---src/main/kotlin/somepackage/SomeLibApplication.kt
|
|---multidemo
| |---build.gradle
| |---settings.gradle
| |---src/main/kotlin/somepackage/SampleController.kt
| |---src/main/kotlin/somepackage/MultidemoApplication.kt
Below are the contents of every file
Root/settings.gradle
include ':somelib', ':multidemo'
somelib/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
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'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
somelib/settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'somelib'
somelib/src/main/kotlin/somepackage/Tester.kt
package com.simbalarry.somelib
/*
* Created on : 17-03-2019
* Author : Nayan Kurude
*/
object Tester {
fun getOutputText(): String {
return "Sample Text"
}
}
somelib/src/main/kotlin/somepackage/SomelibApplication.kt
package com.simbalarry.somelib
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class SomelibApplication
fun main(args: Array<String>) {
runApplication<SomelibApplication>(*args)
}
multidemo/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
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'
compile project(':somelib')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
multidemo/settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'multidemo'
multidemo/src/main/kotlin/somepackage/MultiDemoApplication.kt
package com.simbalarry.multidemo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class MultidemoApplication
fun main(args: Array<String>) {
runApplication<MultidemoApplication>(*args)
}
multidemo/src/main/kotlin/somepackage/SampleController.kt
package com.simbalarry.multidemo
import com.simbalarry.somelib.Tester
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
/*
* Created on : 17-03-2019
* Author : Nayan Kurude
*/
#RestController
class SampleController {
#RequestMapping("/hello")
fun hello(): String {
return Tester.getOutputText()
}
}
Could anyone please tell me where am i doing something wrong.
As I said in comments: somelib is supposed to be a library. So it shouldn't apply the spring boot plugin, whose role is to create a Spring Boot application.
Your build.gradle file for somelib should look like this:
plugins {
id 'java-library' // 1
id 'org.springframework.boot' version '2.1.3.RELEASE' apply false // 2
id 'io.spring.dependency-management' version '1.0.6.RELEASE' //3
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencyManagement { // 4
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
ext['kotlin.version'] = '1.3.21' // 5
dependencies {
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'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
Some explanations (see the comments in the code):
Applies the java library gradle plugin, since that's what you want to create: a library that can be used in the other project
Adds the Spring Boot plugin to the classpath of the build without applying it, in order to be able to use its BOM later. See https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/gradle-plugin/reference/html/#managing-dependencies-using-in-isolation
Applies the spring dependency management plugin to be able to specify dependencies without version, by using the version numbers specified in the spring boot BOM
Configure the dependency management plugin to use the spring boot BOM
Override the kotlin version used in the BOM (1.2.71) by the one you want to use. See https://docs.spring.io/dependency-management-plugin/docs/current-SNAPSHOT/reference/html/#dependency-management-configuration-bom-import-override-property

Spring boot gradle kotlin build

I am trying to create JAVA springboot project with gradle build.I wanted to try out new kotlin support for springboot. Wondering how i can add bootrun task inside build.gradle.kts file.
plugins {
}
val coreServiceVersion = "3.0.0"
dependencies {
}
That's the over all structure of build.gradle.kts
You can use the following configuration:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlinVersion = "1.2.41"
id("org.springframework.boot") version "2.0.2.RELEASE"
id("org.jetbrains.kotlin.jvm") version kotlinVersion
id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
id("io.spring.dependency-management") version "1.0.4.RELEASE"
}
val coreServiceVersion = "3.0.0"
version = "1.0.0-SNAPSHOT"
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjsr305=strict")
}
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
compile("com.fasterxml.jackson.module:jackson-module-kotlin")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
See https://github.com/sdeleuze/spring-boot-kotlin-demo/ for a complete project.

Resources