使用Spring Rest文档记录rxjava单个响应

时间:2019-06-14 17:16:53

标签: rx-java spring-restdocs rspec-api-documentation

我想做的是使用rx java记录rxjava的单个响应,但是我遇到了下一个问题:

 WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class rx.internal.util.ScalarSynchronousSingle.

这是我的控制器的外观:

package com.espn.csemobile.espnapp.controllers

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import rx.Single

@RestController
class HomeController {

    @GetMapping("/")
    fun greeting(): Single<MutableList<Message>> {
        var itemsList: MutableList<Message> = mutableListOf(Message("Hello world"), Message("Hello world2"))
        return Single.just(itemsList)
    }
}
data class Message(val message:String)

这是我的测试

package com.espn.csemobile.espnapp.controllers

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document
import org.springframework.restdocs.operation.preprocess.Preprocessors
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath
import org.springframework.restdocs.payload.PayloadDocumentation.responseFields
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@RunWith(SpringJUnit4ClassRunner::class)
@AutoConfigureRestDocs(outputDir = "target/generated-snippets")
@WebMvcTest(controllers = [HomeController::class], secure = false)
class WebLayerTestOne {

    @Autowired
    private val mockMvc: MockMvc? = null

    @Test
    @Throws(Exception::class)
    fun shouldReturnDefaultMessage() {
        this.mockMvc!!.perform(get("/")).andDo(print()).andExpect(status().isOk)
                .andDo(document("{ClassName}/{methodName}",
                Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
                Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
                responseFields(
                        fieldWithPath("[].message").description("Response Message")
                )))
    }
}

我不确定是否可以使用rxjava记录rxjava单个响应,或者我可以使用哪种方法。

注意:如果没有单一包装,测试将按预期工作,则问题出在Single <>响应上。

0 个答案:

没有答案