spring-bootkotlinspring-boot-testmockkmockk-verify

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


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


Solution

  • 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