Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestAssuredMockMvc Connection to http://localhost:8080 refused

I developed a Spring MVC webapp with rest methods. I would love to use RestAssured to create JUnit test classes. From the documentation it looks really simple to do but I'm having some problem instead. Basically I want to use it to avoid a runtime Tomcat instance but the problem is that when I execute the JUnit test class I obtain the following exception:

org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8082 refused
...
Caused by: java.net.ConnectException: Connection refused

This is my JUnit class:

import static com.jayway.restassured.RestAssured.given;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;

import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/test/resources/spring/application-test-config.xml"})
@WebAppConfiguration
public class ExternalControllerTest {
    private static final Logger logger = LoggerFactory.getLogger(ExternalControllerTest.class);

    @Autowired
    WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
    }

    @Test
    public void addClientDeviceId_Success() {

            given().get("/api/test").then().statusCode(200);

    }

}

I also tried to configure RestAssuredMockMvc directly with the controller class but the result is the same:

RestAssuredMockMvc.standaloneSetup(new ExternalController());

The controller looks like that:

@Controller
@RequestMapping("/api")
public class ExternalController {

    public ExternalController() {
        logger.info("ExternalController initialised!");
    }

    @RequestMapping(value="/test", method = RequestMethod.GET, produces={"application/json", "application/xml"})
    public @ResponseBody DmsResponse test(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_OK);
        return null;
    }

}

In this guide http://www.jayway.com/2014/01/14/unit-testing-spring-mvc-controllers-with-rest-assured/ is specified that "The most important aspect is that it’s simpler to get the environment started. You don’t need to bootstrap a container (for example Jetty or Tomcat), you simply initialize RestAssuredMockMvc with a context (for example a controller) and you’re ready to go." So I don't understand where the problem is. Any suggestion?

The war file deployed with Tomcat works fine. Thanks

like image 408
user3227323 Avatar asked May 21 '14 11:05

user3227323


1 Answers

You are statically importing the wrong methods. You're importing given from com.jayway.restassured.RestAssured (io.restassured.RestAssured if using version 3.x) but you should statically import the methods from com.jayway.restassured.module.mockmvc.RestAssuredMockMvc (io.restassured.module.mockmvc.RestAssuredMockMvc in version 3.x). Refer to the documentation for more info.

like image 100
Johan Avatar answered Nov 03 '22 00:11

Johan