3
votes
import grails.test.mixin.integration.Integration
import spock.lang.Specification

@Integration
class MySpec extends Specification {

   def setup() {
   }

   def cleanup() {
   }

    void "test something"() {
        expect:"tests a"
          true
    }
}

MySpec > test something FAILED
java.lang.IllegalStateException
    Caused by: org.springframework.beans.factory.BeanCreationException
        Caused by: org.springframework.beans.factory.BeanCreationException
            Caused by: org.springframework.beans.factory.BeanCreationException
                Caused by: java.lang.NullPointerException

1 test completed, 1 failed :integrationTest FAILED :mergeTestReports

I run "test-app -integration MySpec" command

Is that an error in grails or I do smth wrong?

UPDATE!

I found a solution - just add testCompile "org.grails.plugins:hibernate" to build.gradle

1
I saw a similar classnotfound error and for me it was solved by adding testCompile 'mysql:mysql-connector-java:8.0.26' to build.gradle - August

1 Answers

-5
votes

Testing was never so easy and intuitive before the use of Spock.We can always test the exceptions in grails Spock Testing. For example we have a method which throws exception like :

String getUserType(int age){
    if(age<=0){
        throw new MyException("Invalid age")
    }else if( age>0 && age<50){
        return "Young"
    }else{
        return "Old"
    }
}

Now we will write the test case of this method for checking whether exception is thrown for invalid inputs or not.

def "exception should be thrown only for age less than or equal to 0"{
    given:
        String type = getUserType(34)
    expect:
        type == "Young"
        notThrown MyException
    when:
        type = getUserType(0)
    then:
        thrown MyException
}

This is how we have tested whether exception is thrown for invalid input or not. Hope it helps