Running JUnit tests on a restart router

Using Restlet I created a router for my Java application.

From using curl, I know that each of the different GET, POST, and DELETE requests works for each of the URIs and returns the correct JSON response.

I want to configure JUnit tests for each of the URIs to simplify the testing process. However, I'm not sure what the best way to query each of the URIs is to get a JSON response that I can compare to make sure the results are expected. Any thoughts on how to do this?

+4
source share
4 answers

You can simply use the Restlet Client to execute queries, and then check each response and its presentation.

For instance:

 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"); 

I'm actually not familiar with JUnit, assert or unit testing in general, so I apologize if there is anything with my example. Hopefully it still illustrates a possible testing approach.

Good luck

+6
source

Testing a ServerResource Device

 // 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); 

where is the router and the @Autowired resource in my test class. Relevant declarations in the context of the Spring application look like

 <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" /> 

And myApplication is like

 public class MyApplication extends Application { } 
+3
source

I got a response to call answering settings in a REST junit test case

 @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()); } 
+1
source

Based on the response Avi Flax I rewrite this code in java and run it with junitparams , a library that allows the passage of parametric tests. The code looks like this:

 @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
source

Source: https://habr.com/ru/post/1302039/


All Articles