Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jersey Rest测试框架和Mockito进行单元测试_Java_Rest_Unit Testing_Jersey_Jersey Test Framework - Fatal编程技术网

Java 使用Jersey Rest测试框架和Mockito进行单元测试

Java 使用Jersey Rest测试框架和Mockito进行单元测试,java,rest,unit-testing,jersey,jersey-test-framework,Java,Rest,Unit Testing,Jersey,Jersey Test Framework,有人能帮我吗。 我正在使用Jersey Rest测试框架版本2.21(在Grizzly容器上)为Rest资源编写单元测试 调试测试类时,我看到myManager的模拟对象。但当调试进入my“MyResource”类时,myManager对象将变为null,并出现null指针异常 我尝试过不同的人给出的解决方案,但没有成功。有人能帮我一下吗。我几乎三天来一直在处理这个问题( 我的资源类是这样的 @Component @Path("/somepath") public class MyResourc

有人能帮我吗。 我正在使用Jersey Rest测试框架版本2.21(在Grizzly容器上)为Rest资源编写单元测试

调试测试类时,我看到myManager的模拟对象。但当调试进入my“MyResource”类时,myManager对象将变为null,并出现null指针异常

我尝试过不同的人给出的解决方案,但没有成功。有人能帮我一下吗。我几乎三天来一直在处理这个问题(

我的资源类是这样的

@Component
@Path("/somepath")
public class MyResource {
    @Autowired
    private MyManager myManager;

    @Path("/somepath")
    @GET
    @Produces("application/json")
    @ResponseType(String.class)
    public Response getResults(@QueryParam("queryParam") String number) {
        // myManager is an interface
        String str = myManager.getResult(number);
    }
}
这是我的测试课

public class MyResourceTest extends JerseyTest {
    @Mock
    private MyManager myManager;

    @InjectMocks
    private MyResource myResource;

    @Override
    protected Application configure() {
        MockitoAnnotations.initMocks(this);
        return new ResourceConfig().register(MyResource.class)
                .register(new AbstractBinder() {
                    @Override
                    protected void configure() {
                        bind(myManager).to(MyManager.class);
                    }
                });
    }

    @Test
    public void getResultsTest() {
        when(myManager.getResult(anyString())).thenReturn(mock(String.class));
        String str = target("path").queryParam("queryParam","10").request().get(String.class);
    }
}
您正在使用Spring(注入)注释,因此将从Spring上下文中查找服务。这就是为什么它为null,因为您尚未在Spring上下文中设置模拟

最好的方法是使用构造函数注入(而不是字段注入),这使得测试更加容易

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

    @Autowired
    public MyResource(MyManager manager) {
        this.manager = manager;
    }
}
那么在你的测试中

return new ResourceConfig()
    .register(new MyResource(myManager));

在MyResource中有MyManager,该对象为null,对吗?感谢您的回复。是的,您是对的。在MyResource类中,模拟对象将变为null。顺便说一句,我使用的是Mockito并且@Mocked annotataion不可用。非常感谢您的回复。我按照您的建议进行了更改,现在可以在MyReso中获得模拟对象urce类。与MyManager类类似,我在MyResource类中也有其他类。我是否应该仅通过Contsructor来构造它们。很抱歉再次询问您。仅出于测试用例目的,我必须包含这些构造函数。是否有其他方法可以做到这一点。hello@Vijaya请单击右勾,如果这个答案对您有所帮助。(y)