使用Restlet我为我的Java应用程序创建了一个路由器。
从使用curl,我知道每个不同的GET,POST& DELETE请求适用于每个URI并返回正确的JSON响应。
我想为每个URI设置JUnit测试,以使测试过程更容易。但是,我不确定向每个URI发出请求以获取JSON响应的最佳方法,然后我可以进行比较以确保结果符合预期。有关如何做到这一点的任何想法?
答案 0 :(得分:7)
您可以使用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
或一般的单元测试,所以如果我的例子中有些内容,我会道歉。希望它仍然可以说明一种可能的测试方法。
答案 1 :(得分: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 {
}
答案 2 :(得分: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());
}
答案 3 :(得分: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());
}
}