使用restito

时间:2015-05-10 08:19:54

标签: mocking rest-assured

我写了一个简单的休息服务,然后尝试通过restito创建存根服务器并使用rest-assured调用服务来测试它。在测试类上运行junit测试时,我得到断言错误,响应返回状态为404而不是200.

当我通过启动本地服务器(在我的情况下是tomcat)测试相同的Rest服务时,它会成功返回响应。

以下是我的代码:

CxfRestService.java

import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("/")
@WebService(name = "employeeService")
public interface CxfRestService {

@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/getemployeedetail")
public Response getEmployeeDetail(
        @QueryParam("employeeId") String employeeId);

}

CxfRestServiceImpl.java

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;

import com.kronos.cxfrestservice.CxfRestService;
import com.kronos.cxfrestservice.dao.EmployeeDao;

public class CxfRestServiceImpl implements CxfRestService {
    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public Response getEmployeeDetail(String employeeId) {
        if ("0".equalsIgnoreCase(employeeId)) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
        return Response.ok(employeeDao.getEmployeeDetails(employeeId)).build();
    }
}

CxfRestServiceTest.java

import static com.jayway.restassured.RestAssured.given;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import javax.ws.rs.core.Response.Status;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.xebialabs.restito.server.StubServer;

public class CxfRestServiceTest {

    private StubServer stubServer;

    @Before
    public void start() {
        stubServer = new StubServer().run();
        RestAssured.port = stubServer.getPort();
        RestAssured.baseURI = "http://localhost";
        RestAssured.basePath = "/CxfRestService/rest/employeeservices";
    }

    @After
    public void stop() {
        if (stubServer != null) {
            stubServer.stop();
        }
    }

    @Test
    public void testGetEmployeeDetail() {

        Response response = given().param("employeeId", "1").when()
            .get("/getemployeedetail");

        String responseData = response.getBody().asString();
        assertEquals("Response should be valid for employee id other than 0",
            Status.OK.getStatusCode(), response.getStatusCode());
    }
}

的beans.xml

<jaxrs:server id="employeeService" address="/employeeservices">
        <jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
        </jaxrs:providers>
        <jaxrs:serviceBeans>
            <ref bean="cxfServiceImpl" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="xml" value="application/xml" />
            <entry key="json" value="application/json" />
        </jaxrs:extensionMappings>
    </jaxrs:server>

   <bean id="cxfServiceImpl" class="com.kronos.cxfrestservice.internal.CxfRestServiceImpl"/>
   <bean id="employeeDao" class="com.kronos.cxfrestservice.dao.EmployeeDao"/>

web.xml 中,CXFServlet映射到url-pattern / rest /*.

我在这个领域很新,所以请告诉我在这里我可能做错了什么或遗漏了什么。

由于

0 个答案:

没有答案