更新时间:2022-10-29 12:13:14
您正在静态导入错误的方法.您要从com.jayway.restassured.RestAssured
(如果使用版本3.x,则是io.restassured.RestAssured
)导入given
,但是应该从com.jayway.restassured.module.mockmvc.RestAssuredMockMvc
(版本3.x中的io.restassured.module.mockmvc.RestAssuredMockMvc
)静态地导入方法.有关更多信息,请参见文档.. >
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
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.
得到错误“获得http:// localhost:9443 / metrics:拨打tcp 127.0.0.1:9443:connect:连接被拒绝”;
org.apache.http.conn.HttpHostConnectException:与 http://localhost 的连接在 android 中被拒绝
Tomcat服务器上的Intellij Web应用程序显示http://localhost:8080/index.jsp而不是http://localhost:8080/myapp/index.jsp
System.Net.HttpListenerException:无法侦听前缀'http://localhost:8080
在 android 4.4.2 版本上获取 net::ERR_CONNECTION_REFUSED (http://localhost:8080)