-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathEmployeeControllerIntegrationTest.java
98 lines (78 loc) · 3.06 KB
/
EmployeeControllerIntegrationTest.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package net.guides.springboot2.springboot2jpacrudexample;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
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.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.HttpClientErrorException;
import net.guides.springboot2.jdbc.Application;
import net.guides.springboot2.jdbc.model.Employee;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EmployeeControllerIntegrationTest {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort
private int port;
private String getRootUrl() {
return "http://localhost:" + port;
}
@Test
public void contextLoads() {
}
@Test
public void testGetAllEmployees() {
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(getRootUrl() + "/employees",
HttpMethod.GET, entity, String.class);
assertNotNull(response.getBody());
}
@Test
public void testGetEmployeeById() {
Employee employee = restTemplate.getForObject(getRootUrl() + "/employees/1", Employee.class);
System.out.println(employee.getFirstName());
assertNotNull(employee);
}
@Test
public void testCreateEmployee() {
Employee employee = new Employee();
employee.setEmailId("admin@gmail.com");
employee.setFirstName("admin");
employee.setLastName("admin");
ResponseEntity<Employee> postResponse = restTemplate.postForEntity(getRootUrl() + "/employees", employee, Employee.class);
assertNotNull(postResponse);
assertNotNull(postResponse.getBody());
}
@Test
public void testUpdateEmployee() {
int id = 1;
Employee employee = restTemplate.getForObject(getRootUrl() + "/employees/" + id, Employee.class);
employee.setFirstName("admin1");
employee.setLastName("admin2");
restTemplate.put(getRootUrl() + "/employees/" + id, employee);
Employee updatedEmployee = restTemplate.getForObject(getRootUrl() + "/employees/" + id, Employee.class);
assertNotNull(updatedEmployee);
}
@Test
public void testDeleteEmployee() {
int id = 2;
Employee employee = restTemplate.getForObject(getRootUrl() + "/employees/" + id, Employee.class);
assertNotNull(employee);
restTemplate.delete(getRootUrl() + "/employees/" + id);
try {
employee = restTemplate.getForObject(getRootUrl() + "/employees/" + id, Employee.class);
} catch (final HttpClientErrorException e) {
assertEquals(e.getStatusCode(), HttpStatus.NOT_FOUND);
}
}
}