2010-02-22 82 views
4

使用Restlet我为我的Java应用程序创建了一个路由器。在Restlet路由器上运行JUnit测试

通过使用curl,我知道每个不同的GET,POST & DELETE请求都适用于每个URI并返回正确的JSON响应。

我想为每个URI设置JUnit测试,以使测试过程更轻松。但是,我不确定向每个URI发出请求的最佳方式,以便获得JSON响应,然后我可以对其进行比较以确保结果符合预期。有关如何做到这一点的任何想法?

+0

我也有类似的问题http://stackoverflow.com/questions/2165561/ways- to-test-restful-services。休息客户端应该适合你的情况。 – Daff 2010-02-22 15:15:23

+0

它很接近,但并不完全一样。如果我可以设置测试套件,那将会很好。同样也会导致团队所有成员需要访问该UI的问题。 – Lee 2010-02-22 15:36:59

回答

6

您可以使用Restlet Client发出请求,然后检查每个响应及其表示。

例如:

Client client = new Client(Protocol.HTTP); 
Request request = new Request(Method.GET, resourceRef); 
Response response = client.handle(request); 

assert response.getStatus().getCode() == 200; 
assert response.isEntityAvailable(); 
assert response.getEntity().getMediaType().equals(MediaType.TEXT_HTML); 

// Representation.getText() empties the InputStream, so we need to store the text in a variable 
String text = response.getEntity().getText(); 
assert text.contains("search string"); 
assert text.contains("another search string"); 

我其实并不熟悉JUnit,assert,或者单元测试一般,所以我道歉,如果有什么东西把我的例子。希望它仍然说明了一种可能的测试方法。

祝你好运!

+0

太棒了。对于使用该示例的其他人来说,断言它是assertTrue(...),但除此之外,它是完美的。谢谢 – Lee 2010-02-25 15:36:27

+0

我的荣幸,很高兴帮助! 顺便说一句,我建议你尝试Groovy这种事情 - 它使测试更简洁。它具有getter和setter快捷键特别好,而==意味着值相等。 因此,您可以编写response.entity.mediaType == MediaType.TEXT_HTML来代替response.getEntity()。getMediaType()。equals(MediaType.TEXT_HTML)。 HTH! – 2010-02-25 18:59:25

3

单元测试ServerResource

// Code under test 
public class MyServerResource extends ServerResource { 
    @Get 
    public String getResource() { 
    // ...... 
    } 
} 

// Test code 
@Autowired 
private SpringBeanRouter router; 
@Autowired 
private MyServerResource myServerResource; 

String resourceUri = "/project/1234"; 
Request request = new Request(Method.GET, resourceUri); 
Response response = new Response(request); 
router.handle(request, response); 
assertEquals(200, response.getStatus().getCode()); 
assertTrue(response.isEntityAvailable()); 
assertEquals(MediaType.TEXT_PLAIN, response.getEntity().getMediaType()); 
String responseString = response.getEntityAsText(); 
assertNotNull(responseString); 

其中router和资源在我的测试类的@Autowired。在Spring应用程序上下文相关的声明看起来像

<bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" /> 
<bean id="myApplication" class="com.example.MyApplication"> 
    <property name="root" ref="router" /> 
</bean> 
<bean name="/project/{project_id}" 
     class="com.example.MyServerResource" scope="prototype" autowire="byName" /> 

而且myApplication类似于

public class MyApplication extends Application { 
} 
0

基础上answer of Avi Flax我这段代码改写为Java和与junitparams运行它,一个库,允许通过参数化测试。代码如下:

@RunWith(JUnitParamsRunner.class) 
public class RestServicesAreUpTest { 

    @Test 
    @Parameters({ 
     "http://url:port/path/api/rest/1, 200, true", 
     "http://url:port/path/api/rest/2, 200, true", }) 
    public void restServicesAreUp(String uri, int responseCode, 
     boolean responseAvaliable) { 
    Client client = new Client(Protocol.HTTP); 
    Request request = new Request(Method.GET, uri); 
    Response response = client.handle(request); 

    assertEquals(responseCode, response.getStatus().getCode()); 
    assertEquals(responseAvaliable, response.isEntityAvailable()); 
    assertEquals(MediaType.APPLICATION_JSON, response.getEntity() 
     .getMediaType()); 

    } 
} 
+0

如何在junit中添加质询请求? – 2014-03-10 08:54:06

1

我在REST JUnit测试用例质询响应设置答案

@Test 
public void test() { 
    String url ="http://localhost:8190/project/user/status"; 
    Client client = new Client(Protocol.HTTP); 
    ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC,"user", "f399b0a660f684b2c5a6b4c054f22d89"); 
    Request request = new Request(Method.GET, url); 
    request.setChallengeResponse(challengeResponse); 
    Response response = client.handle(request); 
    System.out.println("request"+response.getStatus().getCode()); 
    System.out.println("request test::"+response.getEntityAsText()); 
}