Support constructing target object in DataBinder

See gh-26721
This commit is contained in:
rstoyanchev
2023-06-22 20:36:28 +01:00
parent 40bf923d7d
commit ea398d7b7e
11 changed files with 553 additions and 282 deletions

View File

@@ -20,10 +20,12 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.*
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.eq
import org.mockito.BDDMockito.given
import org.mockito.Mockito.mock
import org.springframework.core.MethodParameter
import org.springframework.core.ResolvableType
import org.springframework.core.annotation.SynthesizingMethodParameter
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.support.WebDataBinderFactory
@@ -31,7 +33,6 @@ import org.springframework.web.bind.support.WebRequestDataBinder
import org.springframework.web.context.request.ServletWebRequest
import org.springframework.web.method.support.ModelAndViewContainer
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
import kotlin.annotation.AnnotationTarget.*
/**
* Kotlin test fixture for [ModelAttributeMethodProcessor].
@@ -61,7 +62,11 @@ class ModelAttributeMethodProcessorKotlinTests {
val requestWithParam = ServletWebRequest(mockRequest)
val factory = mock<WebDataBinderFactory>()
given(factory.createBinder(any(), any(), eq("param"), any()))
.willAnswer { WebRequestDataBinder(it.getArgument(1)) }
.willAnswer {
val binder = WebRequestDataBinder(it.getArgument(1))
binder.setTargetType(ResolvableType.forMethodParameter(this.param))
binder
}
assertThat(processor.resolveArgument(this.param, container, requestWithParam, factory)).isEqualTo(Param("b"))
}
@@ -71,7 +76,11 @@ class ModelAttributeMethodProcessorKotlinTests {
val requestWithParam = ServletWebRequest(mockRequest)
val factory = mock<WebDataBinderFactory>()
given(factory.createBinder(any(), any(), eq("param"), any()))
.willAnswer { WebRequestDataBinder(it.getArgument(1)) }
.willAnswer {
val binder = WebRequestDataBinder(it.getArgument(1))
binder.setTargetType(ResolvableType.forMethodParameter(this.param))
binder
}
assertThatThrownBy {
processor.resolveArgument(this.param, container, requestWithParam, factory)
}.isInstanceOf(MethodArgumentNotValidException::class.java)