-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringIntegrationTest.java
More file actions
55 lines (47 loc) · 2.07 KB
/
SpringIntegrationTest.java
File metadata and controls
55 lines (47 loc) · 2.07 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package feature;
import com.xpeho.spring_boot_java_random_user.SpringBootJavaRandomUserApplication;
import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
@CucumberContextConfiguration
@ActiveProfiles("test")
@AutoConfigureTestRestTemplate
@SpringBootTest(
classes = SpringBootJavaRandomUserApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
"spring.sql.init.mode=always",
"spring.sql.init.schema-locations=classpath:schema-h2.sql"
}
)
public class SpringIntegrationTest {
@Autowired
protected TestRestTemplate restTemplate;
@LocalServerPort
protected int port;
protected ResponseEntity<String> latestResponse;
protected void executeGet(String path) {
String url = "http://localhost:" + port + path;
latestResponse = restTemplate.getForEntity(url, String.class);
}
protected void executePost(String path, Object payload) {
String url = "http://localhost:" + port + path;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> request = new HttpEntity<>(payload, headers);
latestResponse = restTemplate.postForEntity(url, request, String.class);
}
protected void executeDelete(String path) {
String url = "http://localhost:" + port + path;
latestResponse = restTemplate.exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, String.class);
}
}