使用Restlet我爲我的Java應用程序創建了一個路由器。在Restlet路由器上運行JUnit測試
通過使用curl,我知道每個不同的GET,POST & DELETE請求都適用於每個URI並返回正確的JSON響應。
我想爲每個URI設置JUnit測試,以使測試過程更輕鬆。但是,我不確定向每個URI發出請求的最佳方式,以便獲得JSON響應,然後我可以對其進行比較以確保結果符合預期。有關如何做到這一點的任何想法?
使用Restlet我爲我的Java應用程序創建了一個路由器。在Restlet路由器上運行JUnit測試
通過使用curl,我知道每個不同的GET,POST & DELETE請求都適用於每個URI並返回正確的JSON響應。
我想爲每個URI設置JUnit測試,以使測試過程更輕鬆。但是,我不確定向每個URI發出請求的最佳方式,以便獲得JSON響應,然後我可以對其進行比較以確保結果符合預期。有關如何做到這一點的任何想法?
您可以使用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
,或者單元測試一般,所以我道歉,如果有什麼東西把我的例子。希望它仍然說明了一種可能的測試方法。
祝你好運!
太棒了。對於使用該示例的其他人來說,斷言它是assertTrue(...),但除此之外,它是完美的。謝謝 – Lee 2010-02-25 15:36:27
我的榮幸,很高興幫助! 順便說一句,我建議你嘗試Groovy這種事情 - 它使測試更簡潔。它具有getter和setter快捷鍵特別好,而==意味着值相等。 因此,您可以編寫response.entity.mediaType == MediaType.TEXT_HTML來代替response.getEntity()。getMediaType()。equals(MediaType.TEXT_HTML)。 HTH! – 2010-02-25 18:59:25
單元測試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 {
}
基礎上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());
}
}
如何在junit中添加質詢請求? – 2014-03-10 08:54:06
我在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());
}
我也有類似的問題http://stackoverflow.com/questions/2165561/ways- to-test-restful-services。休息客戶端應該適合你的情況。 – Daff 2010-02-22 15:15:23
它很接近,但並不完全一樣。如果我可以設置測試套件,那將會很好。同樣也會導致團隊所有成員需要訪問該UI的問題。 – Lee 2010-02-22 15:36:59