ReflectionTestUtils.setField 在 Spring 5.2.2 – Jersey 2 的资源上。

huangapple 未分类评论48阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年5月19日 17:25:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61887637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定