-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControllerApiTest.java
executable file
·34 lines (29 loc) · 1.23 KB
/
ControllerApiTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.api;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class ControllerApiTest {
@LocalServerPort
private int port;
private String hostUrl = ("http://localhost:" + this.port + "/springrest/");
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testHome() throws Exception {
ResponseEntity<String> entity = restTemplate.getForEntity(hostUrl, String.class);
System.out.printf("GET url: %s satus code: %s", hostUrl, entity.getStatusCode());
assertEquals(HttpStatus.OK, entity.getStatusCode());
}
}