Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Grails Spock-从未声明的HrowableException服务引发异常_Java_Unit Testing_Grails_Exception Handling_Spock - Fatal编程技术网

Java Grails Spock-从未声明的HrowableException服务引发异常

Java Grails Spock-从未声明的HrowableException服务引发异常,java,unit-testing,grails,exception-handling,spock,Java,Unit Testing,Grails,Exception Handling,Spock,在运行时,此代码起作用: // Service class class UserService { ApiClient api User create(User user) throws EmailTakenException, UsernameTakenException { User savedUser try { savedUser = api.users.create user setE

在运行时,此代码起作用:

// Service class
class UserService {
    ApiClient api

    User create(User user) throws EmailTakenException, UsernameTakenException {
        User savedUser 

        try {
            savedUser = api.users.create user
            setEnabled savedUser.id, true
            return savedUser
        } 
        catch(ApiException ex) {
            switch(ex.subStatus) {
                case SubStatus.USERS_EMAIL_TAKEN:
                    throw new EmailTakenException()
                    break
                case SubStatus.USERS_USERNAME_TAKEN:
                    throw new UsernameTakenException()
                    break
            }
        }                   
    }   
}
从控制器调用:

// Controller class, an action
def create(CreateCommand cmd) { 
    if(request.get) {
        render view: 'create'
        return
    }

    if(!cmd.validate()) {
        flash.model = cmd
        redirect action: 'create' 
        return
    }

    def user = new User()
    bindData user, params

    try {           
        userService.create user

        flash.success = 'ui.save.success'       
    }
    catch(EmailTakenException ex) {
        flash.model = cmd
        flash.error = 'ui.email.taken'
    }
    catch(UsernameTakenException ex) {
        flash.model = cmd
        flash.error = 'ui.username.taken'
    }       

    redirect action: 'create'       
}
“User”、“SubStatus”和“ApiException”类来自jar库依赖项。当出现问题时,ApiClient抛出ApiException

在运行时,这段代码可以完美地工作,但当我为此编写规范时,它会抛出一个未声明的hrowableexception。以下是斯波克规范:

ApiClient api
UsersApi apiUsers

void setup() {
    api = Mock()
    apiUsers = Mock()
    api.users >> apiUsers

    service.api = api
}

def "create: it should be able to throw an exception when email is already taken"() {
    setup:
    def user = new User(email: 'foo@cod.com', username: 'foo', name: 'Bar Foo')
    def exception = Mock(ApiException)
    exception.subStatus >> SubStatus.USERS_EMAIL_TAKEN

    when:
    service.create user

    then:
    thrown(EmailTakenException) // GrailsException is runtime
    1 * apiUsers.create(_ as User) >> { throw new ApiException(400, SubStatus.USERS_EMAIL_TAKEN, null) }
}

也许你可以用@FailsWith注释重写你的测试?

已经试过了,但效果不好。谢谢你的提示。你用的是哪个版本的Spock?圣杯呢?通常,您必须推断出模拟的类型。我也不会嘲笑这个异常,你可以直接创建一个,而且看起来你并没有使用它,是吗?