gpt4 book ai didi

spring - 将对象属性传递给 spring Mock mvc 调用

转载 作者:行者123 更新时间:2023-11-28 21:37:51 24 4
gpt4 key购买 nike

我正在使用 spring rest docs 生成 API 的文档。我的问题是在 get 中我有一些对象是从其他对象创建的,所以我需要模拟这些对象是创建其他对象的基础。我不太确定我怎样才能做到这一点。这就是我现在正在做的:

我的 Controller 看起来像这样:

import com.espn.api.platform.model.*
import com.espn.csemobile.espnapp.context.*
import com.espn.csemobile.espnapp.context.common.RequestBasedSWIDContext
import com.espn.csemobile.espnapp.context.common.RequestBasedSeeAllContext
import com.espn.csemobile.espnapp.context.common.RequestBasedUIDContext
import com.espn.csemobile.espnapp.models.browseplayer.BrowsePlayerResponse
import com.espn.csemobile.espnapp.services.browseplayer.BrowsePlayerServiceRepresentable
import com.espn.csemobile.espnapp.services.browseplayer.contexts.RequestBrowsePlayerContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import rx.Single

@Controller
@ProductApi(
id = "v1_browse_player",
title = "Browse Player (v1)",
description = "")
class BrowsePlayerController(private val transaction: Transaction,private val apiRequest: ApiRequest) {

@Autowired
lateinit var browsePlayerService: BrowsePlayerServiceRepresentable

@GetRequest(
path = "/v1/browse/players",
timeToLive = 300,
queries = [
QueryParameter(name = "swid", required = true),
QueryParameter(name = "uid"),
QueryParameter(name = "seeAll", type = java.lang.Boolean::class),
QueryParameter(name = "lang", required = true),
QueryParameter(name = "region", required = true),
QueryParameter(name = "version", required = true, type = Integer::class),
QueryParameter(name = "appName", required = true),
QueryParameter(name = "platform", required = true)
]
)
@GetMapping()
fun processBrowsePlayerRequest(transaction: Transaction, apiRequest: ApiRequest): Single<BrowsePlayerResponse?> {
val applicationContext = RequestBasedApplicationContext(apiRequest)
val standardContext = RequestBasedStandardContext(
RequestBasedVersionContext(apiRequest),
applicationContext,
RequestBasedEditionContext(apiRequest, applicationContext),
RequestBasedPlatformContext(apiRequest),
transaction
)
val swidContext = RequestBasedSWIDContext(apiRequest)
val uidContext = if (checkUIDPresent(apiRequest)) RequestBasedUIDContext(apiRequest) else null
val seeAllContext = RequestBasedSeeAllContext(apiRequest)
val requestBrowsePlayerContext = RequestBrowsePlayerContext(standardContext, swidContext, uidContext, seeAllContext, apiRequest)
return browsePlayerService.getEntitiesBrowse(requestBrowsePlayerContext)
}

private fun checkUIDPresent(apiRequest: ApiRequest): Boolean =
apiRequest.parameters["uid"] != null
}

问题是例如当我想创建这个对象时:val swidContext = RequestBasedSWIDContext(apiRequest) 取决于 apiRequest。

我的测试是这样的:

import com.espn.api.platform.model.ApiRequest
import com.espn.api.platform.model.Transaction
import com.espn.csemobile.espnapp.context.StandardContext
import com.espn.csemobile.espnapp.context.common.SWIDContext
import com.espn.csemobile.espnapp.context.common.SeeAllContext
import com.espn.csemobile.espnapp.context.common.UIDContext
import com.espn.csemobile.espnapp.models.UID
import com.espn.csemobile.espnapp.models.browseplayer.*
import com.espn.csemobile.espnapp.services.browseplayer.BrowsePlayerServiceRepresentable
import com.espn.csemobile.espnapp.services.browseplayer.contexts.RequestBrowsePlayerContext
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
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.boot.test.mock.mockito.MockBean
import org.springframework.restdocs.JUnitRestDocumentation
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation
import org.springframework.test.context.junit4.SpringRunner
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.setup.DefaultMockMvcBuilder
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import rx.Single

@RunWith(SpringRunner::class)
@WebMvcTest(BrowsePlayerController::class, secure = false)
@AutoConfigureRestDocs(outputDir = "target/snippets")
class BrowsePlayerControllerTest {

@Autowired
lateinit var mockMvc: MockMvc
@MockBean
private lateinit var browsePlayerServiceRepresentable: BrowsePlayerServiceRepresentable
@MockBean
private lateinit var standardContext: StandardContext
@MockBean
private lateinit var sWDIContext: SWIDContext
@MockBean
private lateinit var uIDContext: UIDContext
@MockBean
private lateinit var seeAllContext: SeeAllContext
@MockBean
private lateinit var apiRequest: ApiRequest
@Autowired
lateinit var context: WebApplicationContext
@MockBean
private lateinit var transaction: Transaction
@get:Rule
var restDocumentation = JUnitRestDocumentation()

@Before
fun init() {
MockitoAnnotations.initMocks(this)
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply<DefaultMockMvcBuilder>(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation)
.uris()
.withScheme("http")
.withHost("localhost")
.withPort(8080))
.build()
}

@Test
@Throws(Exception::class)
fun shouldReturnDefaultMessage() {
val paramsMock = mapOf("swid" to "asudyasd-asdasd-asdasd")
`when`(apiRequest.parameters).thenReturn(paramsMock)
val contextRequest = RequestBrowsePlayerContext(this.standardContext, this.sWDIContext, this.uIDContext, this.seeAllContext, apiRequest)
//`when`(RequestBrowsePlayerContext.(MockMvc., sWDIContext, uIDContext, seeAllContext, apiRequest)).thenReturn(contextRequest)
`when`(browsePlayerServiceRepresentable.getEntitiesBrowse(contextRequest)).thenReturn(Single.just(presentBrowsePlayers()))
`when`(browsePlayerServiceRepresentable.getEntitiesBrowse(contextRequest)).thenReturn(Single.just(presentBrowsePlayers()))
this.mockMvc!!.perform(get("/v1/browse/players/{transaction}{apiRequest}",transaction,apiRequest).param("swid", "asudyasd-asdasd-asdasd")
.param("uid", "s:20~l:23").param("seeAll", true.toString()).param("lang", "es")
.param("region", "us").param("version", "23").param("appName", "espn")
.param("platform", "espn"))
.andDo(print())
}

private fun presentBrowsePlayers(): BrowsePlayerResponse {
var sections = arrayListOf<Node>()
val playersResponse = buildMockSuggestedPlayers()
val nodePlayers = Node(null, SectionType.PLAYERS, playersResponse)
sections.add(nodePlayers)
val entitiesResponse = buildMockEntitiesBrowse()
val nodeEntities = Node(null, SectionType.ENTITIES, entitiesResponse)
sections.add(nodeEntities)
return BrowsePlayerResponse(presentTopHeader(), buildAnalytics(), sections)
}

private fun buildMockSuggestedPlayers(): List<SuggestedPlayer> {
val items = arrayListOf<SuggestedPlayer>()
items.add(SuggestedPlayer(
"http://i.espncdn.com/i/players/130x180/45843.jpg",
"Lionel Messi",
"SOCCER",
"dc5f8d51332b0ab2b4b0c97efdc624e0",
buildAnalyticsPlayer("s:600~t:83", "s:600~l:740", "s:600", SuggestedType.TEAM)
))
items.add(SuggestedPlayer(
"http://i.espncdn.com/i/players/130x180/22774.jpg",
"Cristiano Ronaldo",
"SOCCER",
"19e162c06bf5d817e9f981bf96001ee2",
buildAnalyticsPlayer("s:600~t:111", "s:600~l:730", "s:600", SuggestedType.LEAGUE)
))
return items
}

private fun buildAnalyticsPlayer( teamUid:String?, leagueId :String, sportUid :String, suggestedType:SuggestedType):PlayerAnalytics {
return PlayerAnalytics(teamUid, leagueId, sportUid, suggestedType)
}

private fun presentTopHeader():TopBrowsePlayerHeader {
val searchURL = "http://someurl"
return TopBrowsePlayerHeader("Player Browse", searchURL)
}

private fun buildAnalytics():Analytics {
return Analytics(null, null, null, null, null, PageType.ALL_PLAYERS, false)
}

private fun buildMockEntitiesBrowse(): List<EntityBrowse> {
var items = arrayListOf<EntityBrowse>()
items.add(buildSport("NFL", "s:20~l:28", "http://a.espncdn.com/i/teamlogos/leagues/500/nfl.png"))
items.add(buildSport("NCAA Football", "s:20~l:23", "http://a.espncdn.com/redesign/assets/img/icons/ESPN-icon-football-college.png"))
items.add(buildSport("NBA", "s:40~l:46", "http://a.espncdn.com/i/teamlogos/leagues/500/nba.png"))
items.add(buildSport("NCAA Basketball", "s:40~l:41", "http://a.espncdn.com/redesign/assets/img/icons/ESPN-icon-basketball.png"))
return items
}

private fun buildSport(label: String, uid: String, image: String): EntityBrowse {
val type = "internalLink"
val url = "sportscenter://x-callback-url/showPlayerBrowse?uid=$uid"
val action = BrowsePlayerAction(type, url)
return EntityBrowse(label, UID.create(uid), action, image)
}

我现在真的不知道如何将事务和 apiRequest 模拟对象发送到 Controller ,因为我只调用 this.mockMvc!!.perform(get("/v1/browse/players/{transaction}{apiRequest }",transaction,apiRequest) 不发送任何参数。

有什么想法吗?

最佳答案

我是这样使用 flash 属性的:

@Test
@Throws(Exception::class)
fun shouldReturnDefaultMessage() {
val paramsMock = mapOf("swid" to "asudyasd-asdasd-asdasd","seeAll" to true )
`when`(apiRequest.parameters).thenReturn(paramsMock)
val contextRequest = RequestBrowsePlayerContext(this.standardContext, this.sWDIContext, this.uIDContext, this.seeAllContext, apiRequest)
`when`(browsePlayerServiceRepresentable.getEntitiesBrowse(contextRequest)).thenReturn(Single.just(presentBrowsePlayers()))
`when`(browsePlayerServiceRepresentable.getEntitiesBrowse(contextRequest)).thenReturn(Single.just(presentBrowsePlayers()))
this.mockMvc!!.perform(get("/v1/browse/players").flashAttr("apiRequest", apiRequest))
.andDo(print()).andExpect(status().isOk)
}

因此我可以控制传递给模型的内容,并且可以发送模拟的 apiRequest 对象。

关于spring - 将对象属性传递给 spring Mock mvc 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56349004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com