Unit Testing with Jersey Rest Test Framework and Mockito

蓝咒 提交于 2019-12-01 11:11:59

You're using Spring (injection) annotations, so the service will be looked up from the spring context. That's why it's null, because you haven't set up the mock in the spring context.

The best thing to do is to use constructor injection (instead of field injection). This makes testing a lot easier

@Path(..)
public class MyResource {
    private final MyManager manager;

    @Autowired
    public MyResource(MyManager manager) {
        this.manager = manager;
    }
}

Then in your test

return new ResourceConfig()
    .register(new MyResource(myManager));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!