I am trying to write a JUnit test case that tests a method in a helper class. The method calls an external application using REST, and I am trying to model this call in a JUnit test.
The helper method makes a REST call using the Spring RestTemplate.
In my test, I create a dummy REST server and a dummy REST template and instantiate them as follows:
@Before
public void setUp() throws Exception {
mockServer = MockRestServiceServer.createServer(helperClass.getRestTemplate());
}
Then I start the dummy server so that it returns the appropriate response when the helper method makes a REST call:
// response is some XML in a String
mockServer
.expect(MockRestRequestMatchers.requestTo(new URI(myURL)))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_XML)
.body(response));
When I run my test, the helper method gets a null response from the REST call it makes, and the test fails.
URL REST, , : " http://: //? Queryparam1 = 1 & queryparam2 = 2 ".
URL- (" http://server: port/application/resource ") , "myURL" ( , ), .
, .
4.1.7.
.