Skip to content

Commit a3ff526

Browse files
Merge pull request eugenp#5472 from swapanpramanick2004/BAEL-2221
Bael 2221
2 parents c5d3953 + 5d87e46 commit a3ff526

6 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.web.model;
2+
3+
import java.util.Date;
4+
import java.util.Objects;
5+
6+
public class Employee {
7+
8+
private String id;
9+
private String name;
10+
11+
public Employee(String id, String name) {
12+
this.id = id;
13+
this.name = name;
14+
}
15+
16+
public Employee() {
17+
}
18+
19+
public String getId() {
20+
return id;
21+
}
22+
23+
public void setId(String id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
@Override public boolean equals(Object o) {
36+
if (this == o)
37+
return true;
38+
if (o == null || getClass() != o.getClass())
39+
return false;
40+
Employee employee = (Employee) o;
41+
return Objects.equals(id, employee.id);
42+
}
43+
44+
@Override public int hashCode() {
45+
46+
return Objects.hash(id);
47+
}
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.web.service;
2+
3+
import org.baeldung.web.model.Employee;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.web.client.RestTemplate;
11+
12+
@Service
13+
public class EmployeeService {
14+
15+
static final String EMP_URL_PREFIX = "http://localhost:8080/employee";
16+
static final String URL_SEP = "/";
17+
18+
private static final Logger logger = LoggerFactory.getLogger(EmployeeService.class);
19+
20+
@Autowired
21+
private RestTemplate restTemplate;
22+
23+
public Employee getEmployee(String id) {
24+
25+
ResponseEntity<Employee> resp = restTemplate.getForEntity("http://localhost:8080/employee/" + id,
26+
Employee.class);
27+
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
@Configuration
10+
@EnableAutoConfiguration
11+
@ComponentScan("org.baeldung")
12+
public class SpringTestConfig {
13+
14+
@Bean
15+
public RestTemplate restTemplate() {
16+
return new RestTemplate();
17+
}
18+
19+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.baeldung.web.service;
2+
3+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
4+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
5+
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
6+
7+
import java.net.URI;
8+
9+
import org.baeldung.SpringTestConfig;
10+
import org.baeldung.web.model.Employee;
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.http.HttpMethod;
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.http.MediaType;
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit4.SpringRunner;
23+
import org.springframework.test.web.client.ExpectedCount;
24+
import org.springframework.test.web.client.MockRestServiceServer;
25+
import org.springframework.web.client.RestTemplate;
26+
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
29+
@RunWith(SpringRunner.class)
30+
@ContextConfiguration(classes = SpringTestConfig.class)
31+
public class EmployeeServiceMockRestServiceServerUnitTest {
32+
33+
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceMockRestServiceServerUnitTest.class);
34+
35+
@Autowired
36+
private EmployeeService empService;
37+
38+
@Autowired
39+
private RestTemplate restTemplate;
40+
41+
private MockRestServiceServer mockServer;
42+
43+
private ObjectMapper mapper = new ObjectMapper();
44+
45+
@Before
46+
public void init() {
47+
mockServer = MockRestServiceServer.createServer(restTemplate);
48+
}
49+
50+
@Test
51+
public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_shouldReturnMockedObject() throws Exception {
52+
Employee emp = new Employee("E001", "Eric Simmons");
53+
54+
mockServer.expect(ExpectedCount.once(),
55+
requestTo(new URI("http://localhost:8080/employee/E001")))
56+
.andExpect(method(HttpMethod.GET))
57+
.andRespond(withStatus(HttpStatus.OK)
58+
.contentType(MediaType.APPLICATION_JSON)
59+
.body(mapper.writeValueAsString(emp)));
60+
61+
Employee employee = empService.getEmployee("E001");
62+
mockServer.verify();
63+
Assert.assertEquals(emp, employee);
64+
}
65+
66+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.web.service;
2+
3+
import org.baeldung.web.model.Employee;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.Mockito;
11+
import org.mockito.MockitoAnnotations;
12+
import org.mockito.runners.MockitoJUnitRunner;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.http.ResponseEntity;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
@RunWith(MockitoJUnitRunner.class)
20+
public class EmployeeServiceUnitTest {
21+
22+
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class);
23+
24+
@Mock
25+
private RestTemplate restTemplate;
26+
27+
@InjectMocks
28+
private EmployeeService empService = new EmployeeService();
29+
30+
@Test
31+
public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() throws Exception {
32+
Employee emp = new Employee("E001", "Eric Simmons");
33+
Mockito.when(restTemplate.getForEntity("http://localhost:8080/employee/E001", Employee.class))
34+
.thenReturn(new ResponseEntity(emp, HttpStatus.OK));
35+
36+
Employee employee = empService.getEmployee("E001");
37+
38+
Assert.assertEquals(emp, employee);
39+
}
40+
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
11+
<level value="DEBUG" />
12+
</logger>
13+
14+
<logger name="org.springframework" level="WARN" />
15+
<logger name="org.springframework.transaction" level="WARN" />
16+
17+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
18+
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
19+
20+
<root level="INFO">
21+
<appender-ref ref="STDOUT" />
22+
</root>
23+
</configuration>

0 commit comments

Comments
 (0)