英文:
ReflectionTestUtils.setField on resource spring 5.2.2 - jersey 2
问题
@Path("/lala")
@Component
public class Resource {
@Autowired
private Idservice idService;
@Context
protected HttpServletRequest httpRequest;
}
在测试中将httpRequest设置为MockHttpServletRequest:
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
Resource resource = getContext().getBean(Resource.class);
ReflectionTestUtils.setField(resource, "httpRequest", httpServletRequest);
在测试中:
resource = Resource@1234
httpRequest = MockHttpServletRequest@1111
idService = $Proxy333@3333
在调用api资源时,resource的httpRequest获得另一个对象。
资源对象的地址与测试中的相同。
在resource上:
resource = Resource@1234
httpRequest = $Proxy222@2222
idService = $Proxy333@3333
为什么httpRequest没有保持为MockHttpServletRequest?
在spring 4.1.5和jersey 1中可以正常工作。
英文:
@Path("/lala")
@Component
public class Resource {
@Autowired
private Idservice idService;
@Context
protected HttpServletRequest httpRequest;
}
in test set httpRequest to MockHttpServletRequest:
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
Resource resource = getContext().getBean(Resource.class);
ReflectionTestUtils.setField(resource, "httpRequest", httpServletRequest);
in the test:
resource = Resource@1234
httpRequest = MockHttpServletRequest@1111
idService = $Proxy333@3333
when calling the api resource httpRequest gets another object.
resource object has the same address as in the test.
on resource:
resource = Resource@1234
httpRequest =$Proxy222@2222
idService = $Proxy333@3333
why httpRequest not stay with MockHttpServletRequest ?
it worked on spring 4.1.5 and jersey 1
专注分享java语言的经验与见解,让所有开发者获益!
评论