I developed a Spring MVC web application with rest methods. I would like to use RestAssured to create JUnit test classes. From the documentation, this looks very simple, but instead I have a problem. I basically want to use it to avoid a Tomcat instance at runtime, but the problem is that when I run the JUnit test class, I get the following exception:
org.apache.http.conn.HttpHostConnectException: Connection to http:
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 setting up RestAssuredMockMvc directly with the controller class, but the result is the same:
RestAssuredMockMvc.standaloneSetup(new ExternalController());
The controller looks like this:
@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; } }
This guide http://www.jayway.com/2014/01/14/unit-testing-spring-mvc-controllers-with-rest-assured/ states that "the most important aspect is that it is easier to get started working with the environment. You do not need a bootstrap container (for example, Jetty or Tomcat), you just initialize RestAssuredMockMvc with a context (for example, a controller), and you are ready to work. " Therefore, I do not understand where the problem is. Any suggestion?
The war file deployed with Tomcat works fine. Thanks
source share