Announcement Announcement Module
No announcement yet.
OnSpring 3.2 data rest the integration test gives the error: I/O error on GET request Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • OnSpring 3.2 data rest the integration test gives the error: I/O error on GET request


    I'm trying to code an integration test against a Spring Data Rest controller.

    I have an error message about a connection refused:

    I/O error on GET request for "http://localhost:8080/learnintouch-web/admin/find/11056":Connection refused; nested exception is Connection refused

    when I run an integration test, with the command:

    mvn clean install -Pmysql-db -Pweb

    It feels like my server is not started. I'm thinking so because if I type in the command mvn clean tomcat:run -Pmysql-db and paste the very same url http://localhost:8080/learnintouch-web/admin/find/11056 in a browser then I get a nice json response:

    {"id":3,"version":0,"firstname":"Stephane","lastna me":"Eybert","login":"stephane","password":"toto", "passwordSalt":"","superAdmin":false,"preferenceAd min":false,"address":null,"zipCode":null,"city":nu ll,"country":null,"email":"[email protected]" ,"profile":null,"postLoginUrl":null}
    Here is my controller:
    public class AdminController {
    	@RequestMapping(value = "find/{id}", method = RequestMethod.GET)
    	@Transactional(readOnly = true)
    	public Admin show(@PathVariable("id") Long id) {
    		return adminRepository.findOne(id);
    Here is my integration test:
    @ContextConfiguration(locations = { "classpath:test.xml", "classpath:jpa.xml", "classpath:data-source.xml", "classpath:log4j.xml" })
    public abstract class AbstractControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
        private WebApplicationContext webApplicationContext;
    	protected MockMvc mockMvc;
    	public void setUpTestDataWithinTransaction() {	
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
    public class AdminControllerTest extends AbstractControllerTest {
    	AdminRepository adminRepository;
    	private RestTemplate restTemplate;
    	private Admin admin0;
        public void beforeAnyTest() throws Exception {
            admin0 = new Admin();
            admin0.setEmail("[email protected]");
            admin0 =;
        public void find() {
             String uri = "http://localhost:8080/learnintouch-web/admin/find/{id}";
             Admin admin = restTemplate.getForObject(uri, Admin.class, admin0.getId());
             assertEquals(admin0.getFirstname(), admin.getFirstname());
    Any clue ?
    Last edited by stephaneeybert; May 23rd, 2013, 11:03 AM.

  • #2
    The issue only occurs when using the rest template and not using the mock mvc.
    Last edited by stephaneeybert; May 26th, 2013, 11:51 AM.


    • #3
      I updated the configuration to use a Spring JavaConfig based one instead.

      I still was fighting with an issue when running the integration tests, the web application context not able to be loaded, and could not find the solution.

      I was doing a:
      @ContextConfiguration( loader = AnnotationConfigContextLoader.class , classes = { WebInit.class, ApplicationConfiguration.class })

      But the issue does not show up with a:
      @ContextConfiguration( classes = { WebInit.class, ApplicationConfiguration.class })

      The whole integration test configuration looks like:
      @WebAppConfiguration // TODO this may be a solution (initializer = WebInit.class)
      @ContextConfiguration( classes = { WebInit.class, ApplicationConfiguration.class })
      public abstract class AbstractControllerTest {
      So I can now load the web application context and use a JavaConfig based configuration.

      I suppose this latest issue has not resolved the original one described above in the thread.


      • #4
        With the JavaConfig based configuration, I now have another error, the rest template cannot be loaded:
        Could not autowire field: private org.springframework.web.client.RestTemplate com.thalasoft.learnintouch.web.AdminControllerTest .restTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}


        • #5
          Something I forgot at the time was to start a mock server in the abstract base test class:
          this.mockServer = MockRestServiceServer.createServer(restTemplate);
          And test it like:
          String message = adminRestClient.getGreetingMessage();
          assertThat(message, allOf(containsString("Hello"), containsString("World")));
          Of course, there is a client REST template:
              public String getGreetingMessage() {
                  String message;
                  try {
                      HttpHeaders httpHeaders = Common.createAuthenticationHeaders("stephane" + ":" + "mypassword");
                      ResponseEntity<GreetingResource> responseEntity = restTemplate.getForEntity("/admin/greeting", GreetingResource.class, httpHeaders);
                      GreetingResource greetingResource = responseEntity.getBody();
                      message = greetingResource.getMessage();
                  } catch (HttpMessageNotReadableException e) {
                      message = "The GET request FAILED with the message being not readable: " + e.getMessage();
                  } catch (HttpStatusCodeException e) {
                      message = "The GET request FAILED with the HttpStatusCode: " + e.getStatusCode() + "|" + e.getStatusText();
                  } catch (RuntimeException e) {
                      message = "The GET request FAILED " + ExceptionUtils.getFullStackTrace(e);
                  return message;