簡體   English   中英

在Intellij IDEA中運行時,Grails 2.5.1寧靜的控制器Spock單元測試失敗,並顯示406。

[英]Grails 2.5.1 Restful controller spock unit test fails with 406 when running in Intellij IDEA

我在bitbucket上設置了一個測試應用程序,該應用程序再現了我的問題:

https://bitbucket.org/LuisMuniz/grails-bug-notacceptable

我有相當標准的REST控制器操作(保存),它返回的HTTP響應狀態為201(已創建)。

  • 當我運行功能測試時,一切正常,http狀態為201。
  • 當我使用grailsw獨立運行單元測試時,控制器的行為符合預期,返回代碼201。
  • 當我在Intellij IDEA中運行相同的單元測試(嘗試使用最新的14.x和最新的15個EAP版本)時,控制器返回http代碼406(NOT_ACCEPTABLE)

我已經調試了測試執行,發現它涉及到applicationContext內部的mimeType,它們僅包含一個條目:text / html。

有人知道為什么會這樣嗎? 這是一個已知的問題?

有沒有辦法解決此問題,或者有什么辦法可以使Intellij單元測試不失敗,或者如果在Intellij中執行這些測試,可以使用Junit規則跳過這些測試?

根據要求更新,在此處發布代碼。

package na

import grails.rest.RestfulController
import org.springframework.http.HttpStatus

class MyController extends RestfulController {
    static responseFormats = ['json']
    static allowedMethods = [save: "POST"]

    def save() {
        response.status=HttpStatus.CREATED.value()
        respond request.JSON
    }
}

功能測試(通過):

package na

import grails.util.Holders
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.springframework.context.ApplicationContext
import org.springframework.http.HttpStatus
import spock.lang.Specification
import wslite.rest.RESTClient
/**
 * Created by lmuniz on 17/09/15.
 */
class MyControllerFuncSpec extends Specification {
    def "Controller returns status 201"() {
        given:
        //noinspection GroovyAssignabilityCheck
        def restClient = new RESTClient("http://localhost:8080/notacceptable")

        when:
        def response = restClient.post([path: "/my"]) {
            json payload
        }

        then:
        response.statusCode == HttpStatus.CREATED.value()
        response.json == payload

        where:
        payload = [message: "Hello world"]
    }
}

單元測試(在IDEA中失敗):

package na

import grails.test.mixin.TestFor
import spock.lang.Specification
import spock.lang.Unroll

import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(MyController)
class MyControllerSpec extends Specification {
    boolean runsInIntellij() {
        System.getProperty('idea.launcher.port') != null
    }

    @Unroll
    def "Controller responds with http code #expectedResponseCode when it is running #inEnvironment"(){
        given:
        println System.getProperties().collect {it.toString()}.join('\n')

        request.method = 'POST'
        request.json = [message:"Hello world"]

        when:
        controller.save()

        then:
        response.status == expectedResponseCode

        where:
        expectedResponseCode = (runsInIntellij() ? NOT_ACCEPTABLE.value() : CREATED.value())
        inEnvironment = (runsInIntellij() ? 'inside Intellij' : 'standalone')
    }
}

在您的控制器中,而不是

響應request.JSON

您可以嘗試一下:

渲染(狀態:response.status)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM