Search code examples
spring-boottestinggroovybuild.gradlespock

No tests found for given includes: [com.bright.TwitterAnalog.AuthenticationControllerSpec.Register user with valid request](--tests filter)


I have this build.gradle file

plugins {
    id 'groovy'
    id 'org.springframework.boot' version '3.2.4'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.bright'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '21'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
    implementation 'org.apache.groovy:groovy'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
//  testImplementation 'org.springframework.boot:spring-boot-starter-test'
//  testImplementation 'org.springframework.security:spring-security-test'
    testImplementation platform("org.spockframework:spock-bom:2.3-groovy-4.0")
    testImplementation "org.spockframework:spock-core"
}

tasks.named('test') {
}

and this authentication test class. Im trying to test my registration controller with spock but it doesnt seem to work for me.

package com.bright.TwitterAnalog

import com.bright.TwitterAnalog.controller.AuthenticationController
import com.bright.TwitterAnalog.dto.UserRegistrationRequest
import com.bright.TwitterAnalog.service.AuthenticationService
import org.springframework.http.ResponseEntity
import org.springframework.http.HttpStatus
import spock.lang.Specification


class AuthenticationControllerSpec extends Specification {
    AuthenticationController authenticationController
    AuthenticationService authenticationService

    def setup() {
        authenticationService = Mock(AuthenticationService)
        authenticationController = new AuthenticationController(authenticationService)
    }

    def "Register user with valid request"() {

        given: "User registration details"
        def request = new UserRegistrationRequest(firstname: "John", lastname: "Doe", username: "johndoe", password: "password")

        authenticationService.registerUser(request) >> ResponseEntity.status(HttpStatus.CREATED).build()

        when: "User is authenticated"
        def response = authenticationController.registerUser(request)

        then: "Is status code is the same as created"
        response.statusCode == HttpStatus.CREATED.value()
    }
}

when i ran the test i get the error in the title. What am i doing wrong?

Im relatively new to working with spock testing and springboot apps that use gradle builder and have looked at similar solutions on here but cant seem to find what works for me


Solution

  • Besides that for Spring Boot 3 you should use Spock 2.4-M4, you do not configure the test task or test suite to use JUnit Platform / Spock.

    By default the test task of the default test suite is configured to use JUnit 4, so the Spock tests which are JUnit 5 Platform based are not found.

    Besides that, I recommend you stop using the Spring Dependency Management plugin. It is an obsolete relict from times when Gradle did not have built-in BOM support, by now does more harm than good, and even the maintainer of that plugin recommends not to use it anymore, but instead to use the built-in BOM support using platform(...) as also documented in the Spring Boot docs.