java 使用 restAssured 测试 Spring Boot Rest 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40665315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
testing spring boot rest application with restAssured
提问by klubi
I've been struggling with this for some time now. I'd like to use restAssured to test my SpringBoot REST application.
我已经为此苦苦挣扎了一段时间。我想使用 restAssured 来测试我的 SpringBoot REST 应用程序。
While it looks like container spins up properly, rest assured (and anything else seems to have problems reaching out to it.
虽然看起来容器可以正常旋转,但请放心(其他任何东西似乎都无法接触到它。
All the time I'm getting Connection refused exception.
我一直在收到连接被拒绝的异常。
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
...
my test class:
我的测试课:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SizesRestControllerIT {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void test() {
System.out.println(this.restTemplate.getForEntity("/clothes", List.class));
}
@Test
public void test2() throws InterruptedException {
given().basePath("/clothes").when().get("").then().statusCode(200);
}
}
and now for the weird part, test
passes and prints what it should, but test2
is getting Connection refused exception.
现在对于奇怪的部分,test
通过并打印它应该做的事情,但是test2
连接被拒绝异常。
Any ideas what is wrong with this setup?
任何想法这个设置有什么问题?
回答by klubi
I'll answer this question myself..
这个问题我自己来回答。。
After spending additional amount of time on it it turned out that TestRestTemplate
already knows and sets proper port.
RestAssured does not...
在花费了额外的时间之后,结果证明它TestRestTemplate
已经知道并设置了正确的端口。安心不...
With that I got to a point where below test runs without any issues.
有了这个,我到了下面的测试运行没有任何问题的地步。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SizesRestControllerIT {
@LocalServerPort
int port;
@Before
public void setUp() {
RestAssured.port = port;
}
@Test
public void test2() throws InterruptedException {
given().basePath("/clothes").get("").then().statusCode(200);
}
}
I could have sworn I tried doing it this way previously... But I guess I did use some other annotations with this...
我可以发誓我以前尝试过这样做......但我想我确实使用了其他一些注释......
回答by Mateus Costa
Based on https://stackoverflow.com/users/2838206/klubianswer and to not set the port for every request that you make:
基于https://stackoverflow.com/users/2838206/klubi答案,并且不要为您发出的每个请求设置端口:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SizesRestControllerIT {
@LocalServerPort
int port;
@Before
public void setUp() {
RestAssured.port = port;
}
@Test
public void test2() throws InterruptedException {
given().basePath("/clothes").get("").then().statusCode(200);
}
}
回答by ameet chaubal
are you running on some non-standard port may be? have you tried this in your
您是否在某些非标准端口上运行?你有没有在你的
@Before
public static void init(){
RestAssured.baseURI = "http://localhost"; // replace as appropriate
RestAssured.port = 8080;
}
@Before
public static void init(){
RestAssured.baseURI = "http://localhost"; // replace as appropriate
RestAssured.port = 8080;
}
回答by idmitriev
I'd recommend to use @WebMvcTest
for that case, all you need is to have rest assured mock mvc dependency:
我建议在@WebMvcTest
这种情况下使用,您只需要放心模拟 mvc 依赖项:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
Using of @SpringBootTest
to test just a controller is overhead, all redundant beans, like @Component
, @Service
, etc, will be created and a
full HTTP server will be started. For more details:
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests;
使用@SpringBootTest
测试只是一个控制器的开销,所有多余的豆子一样@Component
,@Service
等等,将被创建和一个完整的HTTP服务器将启动。更多详情:https:
//docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests;
@RunWith(SpringRunner.class)
@WebMvcTest(value = SizesRestController.class)
public class SizesRestControllerIT {
@Autowired
private MockMvc mvc;
@Before
public void setUp() {
RestAssuredMockMvc.mockMvc(mvc);
}
@Test
public void test() {
RestAssuredMockMvc.given()
.when()
.get("/clothes")
.then()
.statusCode(200);
// do some asserts
}
}
回答by Amine Soumiaa
I had the same issue, The server was running the App on the the port 34965 (not 8080).
This solved my problem :
我遇到了同样的问题,服务器在端口 34965(不是 8080)上运行应用程序。
这解决了我的问题:
@Autowired
ServerProperties serverProperties;
@Autowired
Environment environment;
public String getPath() {
final int port = environment.getProperty("local.server.port", Integer.class);
return "http://localhost:" + port;
}
@Before
public void setUp() throws Exception {
RestAssured.baseURI = getPath();
}
@Test
public void testResponse(){
response = get("/books");
}
回答by erik
Simply:
简单地:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)
public class CommonScenarioTest {
@BeforeClass
public static void setup() {
RestAssured.baseURI = "http://localhost/foo";
RestAssured.port = 8090;
}
回答by Issam EL-ATIF
Passing "/clothes"
as parameter to get() method should resolve the issue
"/clothes"
作为参数传递给 get() 方法应该可以解决这个问题
@Test
public void test2() throws InterruptedException {
when().
get("/clothes").
then().
statusCode(200);
}