Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing spring boot rest application with restAssured

Tags:

I've been struggling with this for some time now. I'd like to use restAssured to test my SpringBoot REST application.

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.

Any ideas what is wrong with this setup?

like image 789
klubi Avatar asked Nov 17 '16 21:11

klubi


People also ask

What type of testing is done with REST assured?

Rest assured is java library for testing Restful Web services. It can be used to test XML & JSON based web services. It supports GET, POST, PUT, PATCH, DELETE, OPTIONS and HEAD requests and can be used to validate and verify the response of these requests.


1 Answers

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...

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...

like image 174
klubi Avatar answered Oct 14 '22 20:10

klubi