Announcement Announcement Module
Collapse
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
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Hi,

    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 java.net.ConnectException: 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:
    Code:
    @Controller
    @RequestMapping("admin")
    public class AdminController {
    
    	@RequestMapping(value = "find/{id}", method = RequestMethod.GET)
    	@Transactional(readOnly = true)
    	@ResponseBody 
    	public Admin show(@PathVariable("id") Long id) {
    		return adminRepository.findOne(id);
    	}
    
    }
    Here is my integration test:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:test.xml", "classpath:jpa.xml", "classpath:data-source.xml", "classpath:log4j.xml" })
    @WebAppConfiguration
    public abstract class AbstractControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
    
    	@Autowired
        private WebApplicationContext webApplicationContext;
        
    	protected MockMvc mockMvc;
    	
        @Before
    	public void setUpTestDataWithinTransaction() {	
    	}
    	
        @Before
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
        }
        
    }
    
    public class AdminControllerTest extends AbstractControllerTest {
    
    	@Autowired 
    	AdminRepository adminRepository;
    
    	@Autowired 
    	private RestTemplate restTemplate;
    
    	private Admin admin0;
    	
        @Before
        public void beforeAnyTest() throws Exception {
            admin0 = new Admin();
            admin0.setLogin("stephane");
            admin0.setPassword("toto");
            admin0.setPasswordSalt("");
            admin0.setEmail("[email protected]");
            admin0.setFirstname("Stephane");
            admin0.setLastname("Eybert");
    
            admin0 = adminRepository.save(admin0);
        }
    
    	@Test
        public void find() {
             String uri = "http://localhost:8080/learnintouch-web/admin/find/{id}";
             Admin admin = restTemplate.getForObject(uri, Admin.class, admin0.getId());
             assertNotNull(admin);
             assertEquals(admin0.getFirstname(), admin.getFirstname());
        }
    	
    }
    Any clue ?
    Last edited by stephaneeybert; May 23rd, 2013, 12:03 PM.

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

    Comment


    • #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:
      Code:
      @RunWith(SpringJUnit4ClassRunner.class)
      @WebAppConfiguration // TODO this may be a solution (initializer = WebInit.class)
      @ContextConfiguration( classes = { WebInit.class, ApplicationConfiguration.class })
      @Transactional
      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.

      Comment


      • #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)}

        Comment


        • #5
          Something I forgot at the time was to start a mock server in the abstract base test class:
          Code:
          this.mockServer = MockRestServiceServer.createServer(restTemplate);
          And test it like:
          Code:
          mockServer.expect(requestTo("/admin/greeting")).andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.OK));
          String message = adminRestClient.getGreetingMessage();
          mockServer.verify();
          assertThat(message, allOf(containsString("Hello"), containsString("World")));
          Of course, there is a client REST template:
          Code:
              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;
              }

          Comment

          Working...
          X