简体   繁体   中英

Jersey 1.19 Test configuration - mock classes

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?

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM