Testing spring applications to download with restAssured

I struggled with this for some time. I would like to use restAssured to test my RESTBEST REST application.

As long as the container seems to rotate correctly, be sure (and something else has problems associated with it.

All the time when I get Connection, I threw an 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 gets a Connection rejection.

Any ideas what is wrong with this setting?

+14
java spring rest spring-boot rest-assured
source share
7 answers

I myself will answer this question.

After spending extra time, it turned out that TestRestTemplate already knows and sets the correct port. RestAssured not ...

With this, I reached the point where the test below passes without any problems.

 @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 to do this before ... But I probably used some other annotations with this ...

+28
source share

Based on https://stackoverflow.com/users/2838206/klubi, answer and do not set a port for each request you make:

 @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); } } 
+5
source share

Perhaps you work on some non-standard port? have you tried it in your

@Before public static void init(){ RestAssured.baseURI = "http://localhost"; // replace as appropriate RestAssured.port = 8080; }

+3
source share

I would recommend using @WebMvcTest for this case, all you need to do is ensure that mkc is reliably dependent on mvc:

 <dependency> <groupId>com.jayway.restassured</groupId> <artifactId>spring-mock-mvc</artifactId> <version>${rest-assured.version}</version> <scope>test</scope> </dependency> 

Using @SpringBootTest to check only the controller is overhead, all redundant beans, such as @Component , @Service , etc., will be created and a full HTTP server will be launched. More details: 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 } } 
+1
source share

Just:

 @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; } 
0
source share

I had the same problem: the server had an App on port 34965 (not 8080).

This solved my problem:

 @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"); } 
0
source share

Passing "/clothes" as a parameter to the get () method should solve the problem

 @Test public void test2() throws InterruptedException { when(). get("/clothes"). then(). statusCode(200); } 
-one
source share

All Articles