在@WebMvcTest
单元测试期间,如何将PersistentEntityResourceAssembler正确地注入到自定义REST控制器的请求方法中?
第一个SO问题。事先道歉,可能只是缺少一些愚蠢的东西。
在GitHub上可用的示例代码。
Oliver Gierke,您在哪里? :P
我有一个用@RepositoryRestController
注释的自定义REST控制器。它的某些请求方法已插入PersistentEntityResourceAssembler
作为参数,因此我可以返回HAL资源。
@RepositoryRestController
@RestController
@RequestMapping(produces = MediaTypes.HAL_JSON_VALUE )
public class ReportRestController {
private final ReportRepository reportRepository;
public ReportRestController(ReportRepository reportRepository) {
this.reportRepository = reportRepository;
}
@GetMapping(path = "/custom/reports/{id}")
public ResponseEntity<?> customReportsEndpoint(@PathVariable("id") Long id,
PersistentEntityResourceAssembler entityAssembler) {
return ResponseEntity.ok(entityAssembler.toResource(reportRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("No report found with ID: " + id))));
}
}
如果我正在运行 集成测试 (@SpringBootTest
),那么效果很好:
import static org.hamcrest.CoreMatchers.*;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CustomRestControllerIntegrationTest {
private static final String REPORT_NAME = "Report 1";
@MockBean
private ReportRepository repository;
@Autowired
private MockMvc mvc;
@Test
public void thisTestPasses() throws Exception {
given(repository.findById(any())).willReturn(Optional.of(new Report(1L, REPORT_NAME, new User(1L, "pbriggs"))));
mvc.perform(get("/custom/reports/1"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", equalTo(REPORT_NAME)))
.andExpect(jsonPath("$._links.enteredBy").exists())
.andReturn().getResponse();
}
}
但是,如果我正在运行 单元测试 (@WebMvcTest
),则由于{entity}不存在,无法构建PersistentEntityResourceAssembler
注射正确。
原因:org.springframework.beans.BeanInstantiationException: 无法实例化 [org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler]: 构造函数抛出异常;嵌套异常为 java.lang.IllegalArgumentException:实体被标记为@NonNull但 为空
GitHub中有完整的堆栈跟踪信息(已达到字符数限制)
代码:
import static org.hamcrest.Matchers.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebMvcTest
@EnableSpringDataWebSupport
public class CustomRestControllerUnitTest1 {
@Autowired
private MockMvc mvc;
@MockBean
private ReportRepository repository;
@Test
public void thisTestFails() throws Exception {
Report report = new Report(1L,"Report 1", new User(1L,"pbriggs"));
given(repository.findById(1L)).willReturn(Optional.of(report));
// Fails with:
//
// Caused by: org.springframework.beans.BeanInstantiationException:
// Failed to instantiate [org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler]:
// Constructor threw exception; nested exception is java.lang.IllegalArgumentException:
// entities is marked @NonNull but is null
MvcResult mvcResult = mvc.perform(get("/custom/reports/1").accept(MediaTypes.HAL_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("Report 1")))
.andReturn();
}
}
我认为这是因为RepositoryRestMvcAutoConfiguration
(因此又RepositoryRestMvcConfiguration
)从未加载过。对于@WebMvcTest
,这是正确的行为,因为它仅加载与Web相关的bean(@ Controller,@ ControllerAdvice,@ JsonComponent,Converter / GenericConverter,Filter,WebMvcConfigurer和HandlerMethodArgumentResolver)。
另外,在GitHub上的示例代码中运行单元测试时,我得到堆栈溢出的时间大约是一半。 Kinda很奇怪,因为它非常不确定。.
要重复一遍,如何在PersistentEntityResourceAssembler
单元测试期间将@WebMvcTest
正确地注入到自定义REST控制器的请求方法中?
_links
部分,等等)@EnableSpringDataWebSupport
StackOverflow问题:How do I set up unit tests for RepositoryRestController in Spring?
此问题/答案建议添加@EnableSpringDataWebSupport
,但这使我的控制器永远无法实例化。也许是因为它使用了@Configuration
并且单元测试没有在完整的容器中运行?我不知道。
更新10/28/18:上述问题还导致在JIRA中创建了Documentation Request。
PersistentEntityResourceAssembler
更改为RepositoryEntityLinks
StackOverflow问题:Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler
对此answer的question建议将PersistentEntityResourceAssembler
更改为RepositoryEntityLinks
并手动生成链接。我需要像使用PersistentEntityResourceAssembler
@BasePathAwareController
添加到控制器StackOverflow问题:JUnit for RestController with parameter PersistentEntityResourceAssembler
其中一个答案将我引向DATAREST-657,建议添加@BasePathAwareController
,但这并没有改变。
PersistentEntityResourceAssembler
StackOverflow问题:Rest Controllers vs spring-data-rest RepositoryRestResource
此answer建议嘲笑PersistentEntityResourceAssembler
和其他内容,但我无法使其正常工作。我会收到以下异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.mockito.internal.junit.DefaultStubbingLookupListener]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.junit.DefaultStubbingLookupListener and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.rest.webmvc.PersistentEntityResource["persistentEntity"]->org.springframework.data.mapping.PersistentEntity$MockitoMock$2115290768["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]->org.mockito.internal.handler.InvocationNotifierHandler["mockSettings"]->org.mockito.internal.creation.settings.CreationSettings["stubbingLookupListeners"]->java.util.ArrayList[0])
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:165)
at com.prestonb.edu.CustomRestControllerUnitTest2.thisTestFails(CustomRestControllerUnitTest2.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.junit.JUnitRule$1.evaluateSafely(JUnitRule.java:52)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:43)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.mockito.internal.junit.DefaultStubbingLookupListener]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.junit.DefaultStubbingLookupListener and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.rest.webmvc.PersistentEntityResource["persistentEntity"]->org.springframework.data.mapping.PersistentEntity$MockitoMock$2115290768["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]->org.mockito.internal.handler.InvocationNotifierHandler["mockSettings"]->org.mockito.internal.creation.settings.CreationSettings["stubbingLookupListeners"]->java.util.ArrayList[0])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:291)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:102)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:272)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:224)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
... 34 more
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.junit.DefaultStubbingLookupListener and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.rest.webmvc.PersistentEntityResource["persistentEntity"]->org.springframework.data.mapping.PersistentEntity$MockitoMock$2115290768["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]->org.mockito.internal.handler.InvocationNotifierHandler["mockSettings"]->org.mockito.internal.creation.settings.CreationSettings["stubbingLookupListeners"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:312)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:285)
... 45 more
org.springframework.data.rest.webmvc.PersistentEntityResource["persistentEntity"]
->org.springframework.data.mapping.PersistentEntity$MockitoMock$1659608278["mockitoInterceptor"]
->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]
->org.mockito.internal.handler.InvocationNotifierHandler["mockSettings"]
->org.mockito.internal.creation.settings.CreationSettings["stubbingLookupListeners"]
->java.util.ArrayList[0])
代码:
import static org.hamcrest.CoreMatchers.*;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class CustomRestControllerUnitTest2 {
@Mock
private PersistentEntityResourceAssembler assembler;
@Mock
private PersistentEntityResourceAssemblerArgumentResolver assemblerResolver;
@Mock
private PersistentEntity<Report, ?> entity;
@InjectMocks
private ReportRestController controller;
private MockMvc mvc;
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private ReportRepository repository;
@Before
public void setup() {
this.mvc = MockMvcBuilders.standaloneSetup(controller)
.setCustomArgumentResolvers(assemblerResolver)
.build();
}
@Test
public void thisTestFails() throws Exception {
Report report = new Report(1L,"Report 1", new User(1L,"pbriggs"));
given(repository.findById(1L)).willReturn(Optional.of(report));
given(assemblerResolver.supportsParameter(any())).willReturn(true);
given(assemblerResolver.resolveArgument(any(), any(), any(), any())).willReturn(assembler);
given(assembler.toResource(report)).willReturn(PersistentEntityResource.build(report, entity).build());
MvcResult mvcResult = mvc.perform(get("/custom/reports/1").accept(MediaTypes.HAL_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("Report 1")))
.andExpect(jsonPath("$.name").exists())
.andExpect(jsonPath("$._links.enteredBy").exists())
.andReturn();
}
}
因此,我随后向MockMvc添加了一个自定义参数解析器,该解析器解决了该问题,但我的JSON不再正确格式化为HAL:
@Test
public void thisTestFails() throws Exception {
Report report = new Report(1L,"Report 1", new User(1L,"pbriggs"));
given(repository.findById(1L)).willReturn(Optional.of(report));
given(assemblerResolver.supportsParameter(any())).willReturn(true);
given(assemblerResolver.resolveArgument(any(), any(), any(), any())).willReturn(assembler);
given(assembler.toResource(report)).willReturn(PersistentEntityResource.build(report, entity).build());
MvcResult mvcResult = mvc.perform(get("/custom/reports/1").accept(MediaTypes.HAL_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("Report 1")))
.andExpect(jsonPath("$.name").exists())
// Fails:
//
// Caused by: com.jayway.jsonpath.PathNotFoundException: Missing property in path $['_links']
.andExpect(jsonPath("$._links.enteredBy").exists())
.andReturn();
/*
* Expected (HAL document):
*
* {
* "name" : "Report 1",
* "_links" : {
* "self" : {
* "href" : "http://localhost/reports/1"
* },
* "report" : {
* "href" : "http://localhost/reports/1"
* },
* "enteredBy" : {
* "href" : "http://localhost/reports/1/enteredBy"
* }
* }
* }
*
* Actual (Normal json):
*
* {
* "id": 1,
* "name": "Report 1",
* "enteredBy": {
* "id": 1,
* "username": "pbriggs"
* }
* // plus a bunch of mockito properties
* }
*/
}