import
static
org.junit.Assert.assertEquals;
import
static
org.springframework.test.web.client.match.RequestMatchers.method;
import
static
org.springframework.test.web.client.match.RequestMatchers.requestTo;
import
static
org.springframework.test.web.client.response.ResponseCreators.withSuccess;
import
org.junit.Before;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.http.HttpMethod;
import
org.springframework.http.MediaType;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
org.springframework.test.web.client.MockRestServiceServer;
import
org.springframework.web.client.RestTemplate;
import
com.waheed.mockserver.controller.RestController;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="file:WebContent/WEB-INF/spring-servlet.xml")
public
class
TestMockRestServiceServer {
@Autowired
private
RestTemplate restTemplate;
private
MockRestServiceServer mockServer;
@Autowired
RestController
restController;
//
Execute the Setup method before the test.
@Before
public
void
setUp() {
//create
a mock Server instance for RestTemplate
mockServer
= MockRestServiceServer.createServer(restTemplate);
}
@Test
public
void
testGoogleSuccess() {
mockServer
.expect(requestTo("http://google.com"))
.andExpect(method(HttpMethod.GET))
.andRespond(
withSuccess("SUCCESS",
MediaType.TEXT_PLAIN));
String
response = restController.hitGoogle();
mockServer.verify();
assertEquals("SUCCESS",response);
}
}