1
votes

Currently searching for tutorials, explanations and examples. I've tried different examples and came up to different errors. My current error is :

| Error Compilation error compiling [unit] tests: startup failed:

and in my test reports. It outputs this :

Unit Test Results - Summary No tests executed.

My "UserSpec.groovy" code is this :

package newmyproject245

import grails.test.mixin.*
import spock.lang.Specification

@TestFor(User)
class UserSpec extends ConstraintSpecification {

    def setup() {
        Expectations.applyTo User
    }

    def cleanup() {
    }

    void testShouldDoNothing() {
        Expectations.applyTo User

        user."password is not blank"
        user."password is not nullable"
        user."name is not blank"
        user."name is not nullable"
    }

    void testEventNameConstraints() {
        Expectations.applyTo User
        def user = new User()

        user."name is not blank"
        user."name is not nullable"
    }
}

Can anybody help. I'm new in grails. Thanks!

Additional to the above problem, when I omitted the Contraints in class as shown :

class UserSpec extends Specification {

I came up to this error :

| Running 1 unit test... 1 of 1 | Failure: initializationError(org.junit.runner.manipulation.Filter) | java.lang.Exception: No tests found matching grails test target pattern filter from org.junit.runner.Request$1@12c27788 at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35) at org.junit.runner.JUnitCore.run(JUnitCore.java:138) | Completed 1 unit test, 1 failed in 0m 0s | Error Fatal error running tests: Not-null property references a transient value - transient instance must be saved before current operation : newmyproject245.Order.product -> newmyproject245.Product; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : newmyproject245.Order.product -> newmyproject245.Product (Use --stacktrace to see the full trace)

someone help. Again, Thanks!

1

1 Answers

0
votes

I already got the answer. See code for reference :

UserSpec.groovy

package project101

import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
 */
@TestMixin(GrailsUnitTestMixin)
@TestFor(User)
class UserSpec extends Specification {

    def user

    def setup() {
        user = new User(firstName: 'FIRSTNAME', lastName: 'LASTNAME', address: 'Finland', username: 'user1', password: 'pass123', userType: 'ADMIN')

    }

    def cleanup() {
        user = null
    }

    void "Test if User handles"() {
        given:
            setup()
        when: "User field has null value"
            user?.username = null
        then: "Validation returns false"
            user?.validate() == false
            user?.errors?.hasFieldErrors('username') == true
    }
}

And make sure that the dbCreate of the test environment is "create-drop" to avoid such errors. Found in DataSource.groovy

test {
        dataSource {
            pooled = true
            dbCreate = "create-drop"

Regards,

Thanks! (^_~)