Jersey 1.19 Test configuration - mock classes

做~自己de王妃 提交于 2019-12-11 04:47:44

问题


I want to test my REST service with:

<dependency>
   <groupId>com.sun.jersey.jersey-test-framework</groupId>
   <artifactId>jersey-test-framework-grizzly2</artifactId>
   <version>1.19</version>
   <scope>test</scope>
</dependency>

I have configuration class:

public class MyServiceTest extends JerseyTest {

    @Override
    protected int getPort(int defaultPort) {
        return 8080;
    }

    public static class AppConfig extends DefaultResourceConfig {
        public AppConfig() {
            super(MyService.class);
        }
    }

    @Override
    public WebAppDescriptor configure() {
        return new WebAppDescriptor.Builder()
        .initParam(WebComponent.RESOURCE_CONFIG_CLASS, 
                AppConfig.class.getName())
                .build();
    }

    public MyServiceTest(){

    }
}

MyService.java is REST endpoint which has injected DAO and other services. There are setters for them in MyService.java for mocking purposes. How to provide MyService instance with set/mocked related classes?


回答1:


It worked with

public static class AppConfig extends DefaultResourceConfig {
    public AppConfig() {

        MyService myService = new MyService();
        MyDAO myDAO = mock(MyDAO.class);
        myService.setMyDAO(myDAO);

        getSingletons().add(new ExceptionMapperProvider()); 
        getSingletons().add(myService);
    }
}


来源:https://stackoverflow.com/questions/37965547/jersey-1-19-test-configuration-mock-classes

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