【问题标题】:JUnit RESTeasy service with a mocked data layer... how do I mock it?带有模拟数据层的 JUnit RESTeasy 服务......我如何模拟它?
【发布时间】:2012-05-15 02:16:57
【问题描述】:

我正在尝试为 RESTeasy Web 服务编写 JUnit 测试用例。我想为此使用 RESTeasy MockDispatcherFactory,同时不依赖任何数据访问层。

在我之前的测试用例创作中,我使用 Mockito 来模拟数据访问,但是我在使用 RESTeasy 的 MockDispatcherFactory 时遇到了麻烦...

服务类:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("")
public class Service {
private StubDao stubDao;
    public Service (){
        this.stubDao = new StubDao();
    }

    public Service (StubDao stubDao){
        this.stubDao = stubDao;
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String get(){
        return stubDao.getTheValue();
    }
}

数据访问:

public class StubDao {
    private String value;

    public StubDao(){

    }

    public String getTheValue(){
        //Stubbed Data Access
        return value;
    }

    public void setTheValue(String v){
        this.value = v;
    }
}

单元测试:

import java.net.URISyntaxException;

import junit.framework.Assert;

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory;
import org.junit.Test;


public class TestService {
@Test
public void testService() throws URISyntaxException{

        POJOResourceFactory factory = new POJOResourceFactory(Service.class);
    //I Need to use Mockito to mock the StubDao object!!!       

    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
    dispatcher.getRegistry().addResourceFactory(factory); 

        MockHttpRequest request = MockHttpRequest.get("");
    MockHttpResponse response = new MockHttpResponse();

    //here my exception is thrown
    dispatcher.invoke(request, response);

    System.out.println(response.getContentAsString());

    // but I expect the response to be 404 (which works outside the mock setup      
    Assert.assertEquals(response.getStatus(), 404);
}
}

通常我会使用 Mockito 来模拟数据访问,如下所示:

设置模拟

@Before
public void setup() {
    StubDao stubDao = new StubDao();
}

定义模拟

when(stubDao.getTheValue()).thenReturn("the mocked value");

但是,RESTeasy 的模拟在内部创建了服务类的新实例。我的问题是,如何将模拟的数据访问插入到服务的构造函数中???

感谢任何帮助!

【问题讨论】:

    标签: junit mocking resteasy


    【解决方案1】:

    感谢另一篇帖子 (Resteasy Server-side Mock Framework) 找到了答案

    使用以下内容允许我创建服务类的实例并设置数据访问权限:

    dispatcher.getRegistry().addSingletonResource(svc);
    

    代替:

    dispatcher.getRegistry().addResourceFactory(factory);
    

    【讨论】:

      【解决方案2】:

      或者,您可以使用 testfun-JEE 在测试中运行轻量级 JAX-RS(基于 RESTeasy 和 TJWS),并使用 testfun-JEE 的 JaxRsServer junit 规则来构建 REST 请求和断言响应。

      testfun-JEE 支持将其他 EJB 以及 mockito 模拟对象注入到您的 JAX-RS 资源类中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-24
        • 1970-01-01
        • 2021-12-10
        • 2019-04-19
        • 2016-01-25
        • 2015-09-23
        • 2020-06-09
        • 1970-01-01
        相关资源
        最近更新 更多