Вопрос

in my services folder, i have this file

abstract class AsyncUploadedFileService<T> {

    def grailsApplication
    def jesqueService

    String jobName
    String workerPool
    String _queueName

    AsyncUploadedFileService (Class<T> job, String workerPool) {
        jobName = job.simpleName
        this.workerPool = workerPool
    }
}

and I have a unit test

@Build(UploadedFile)
@TestMixin(GrailsUnitTestMixin)
@TestFor(AsyncUploadedFileService)
class AsyncUploadedFileServiceSpec extends Specification {
    def setup() {
        User.metaClass.encodePassword = {-> }
    }

    void "add to the worker pool"() {
        when:
        UploadedFile uploadedFile = UploadedFile.build()

        then:
        service.perform(uploadedFile)

    }
}

when I run my test, I get the following error

Cannot add Service class [class com.example.AsyncUploadedFileService]. It is not a Service!
org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Cannot add Service class [class com.example.AsyncUploadedFileService]. It is not a Service!
at grails.test.mixin.services.ServiceUnitTestMixin.mockService(ServiceUnitTestMixin.groovy:46)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)

Is this error because I'm using the service.perform() syntax? If so, what other way is there of testing the AsynchUploadedFileService function?

Это было полезно?

Решение

You can't instantiate an abstract class, so you can't create one to test with. Make it non-abstract, or create a concrete subclass and test that.

Also, since you have a constructor with arguments, there is no default (no-arg) constructor. If you have no constructors, the compiler adds one for you (in both Java and Groovy) and in Grails since services are auto-registered, you must have a no-arg constructor (you can have others, but I can't see why you would) so Spring can create the instance. You can always manually wire up a class as a Spring bean and provide constructor args in resources.groovy, but then it wouldn't be a Grails service, so it would be better to put it in src/groovy.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top