Skip to content

Commit 393a474

Browse files
adding controller advice
1 parent 56a68de commit 393a474

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package guru.springfamework.controllers;
2+
3+
import guru.springfamework.services.ResourceNotFoundException;
4+
import org.springframework.http.HttpHeaders;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.context.request.WebRequest;
10+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
11+
12+
/**
13+
* Created by jt on 10/6/17.
14+
*/
15+
@ControllerAdvice
16+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
17+
18+
@ExceptionHandler({ResourceNotFoundException.class})
19+
public ResponseEntity<Object> handleNotFoundException(Exception exception, WebRequest request){
20+
21+
return new ResponseEntity<Object>("Resource Not Found", new HttpHeaders(), HttpStatus.NOT_FOUND);
22+
23+
}
24+
}

src/main/java/guru/springfamework/services/CustomerServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public CustomerDTO getCustomerById(Long id) {
4747
customerDTO.setCustomerUrl(getCustomerUrl(id));
4848
return customerDTO;
4949
})
50-
.orElseThrow(RuntimeException::new); //todo implement better exception handling
50+
.orElseThrow(ResourceNotFoundException::new);
5151
}
5252

5353
@Override
@@ -92,7 +92,7 @@ public CustomerDTO patchCustomer(Long id, CustomerDTO customerDTO) {
9292

9393
return returnDto;
9494

95-
}).orElseThrow(RuntimeException::new); //todo implement better exception handling;
95+
}).orElseThrow(ResourceNotFoundException::new);
9696
}
9797

9898
private String getCustomerUrl(Long id) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package guru.springfamework.services;
2+
3+
/**
4+
* Created by jt on 10/6/17.
5+
*/
6+
public class ResourceNotFoundException extends RuntimeException {
7+
8+
public ResourceNotFoundException() {
9+
}
10+
11+
public ResourceNotFoundException(String message) {
12+
super(message);
13+
}
14+
15+
public ResourceNotFoundException(String message, Throwable cause) {
16+
super(message, cause);
17+
}
18+
19+
public ResourceNotFoundException(Throwable cause) {
20+
super(cause);
21+
}
22+
23+
public ResourceNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
24+
super(message, cause, enableSuppression, writableStackTrace);
25+
}
26+
}

src/test/java/guru/springfamework/controllers/v1/CategoryControllerTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package guru.springfamework.controllers.v1;
22

33
import guru.springfamework.api.v1.model.CategoryDTO;
4+
import guru.springfamework.controllers.RestResponseEntityExceptionHandler;
45
import guru.springfamework.services.CategoryService;
6+
import guru.springfamework.services.ResourceNotFoundException;
57
import org.junit.Before;
68
import org.junit.Test;
79
import org.mockito.InjectMocks;
@@ -38,7 +40,9 @@ public class CategoryControllerTest {
3840
public void setUp() throws Exception {
3941
MockitoAnnotations.initMocks(this);
4042

41-
mockMvc = MockMvcBuilders.standaloneSetup(categoryController).build();
43+
mockMvc = MockMvcBuilders.standaloneSetup(categoryController)
44+
.setControllerAdvice(new RestResponseEntityExceptionHandler())
45+
.build();
4246

4347
}
4448

@@ -75,4 +79,14 @@ public void testGetByNameCategories() throws Exception {
7579
.andExpect(status().isOk())
7680
.andExpect(jsonPath("$.name", equalTo(NAME)));
7781
}
82+
83+
@Test
84+
public void testGetByNameNotFound() throws Exception {
85+
86+
when(categoryService.getCategoryByName(anyString())).thenThrow(ResourceNotFoundException.class);
87+
88+
mockMvc.perform(get(CategoryController.BASE_URL + "/Foo")
89+
.contentType(MediaType.APPLICATION_JSON))
90+
.andExpect(status().isNotFound());
91+
}
7892
}

src/test/java/guru/springfamework/controllers/v1/CustomerControllerTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package guru.springfamework.controllers.v1;
22

33
import guru.springfamework.api.v1.model.CustomerDTO;
4+
import guru.springfamework.controllers.RestResponseEntityExceptionHandler;
45
import guru.springfamework.services.CustomerService;
6+
import guru.springfamework.services.ResourceNotFoundException;
57
import org.junit.Before;
68
import org.junit.Test;
79
import org.mockito.InjectMocks;
@@ -37,7 +39,9 @@ public class CustomerControllerTest extends AbstractRestControllerTest {
3739
public void setUp() throws Exception {
3840
MockitoAnnotations.initMocks(this);
3941

40-
mockMvc = MockMvcBuilders.standaloneSetup(customerController).build();
42+
mockMvc = MockMvcBuilders.standaloneSetup(customerController)
43+
.setControllerAdvice(new RestResponseEntityExceptionHandler())
44+
.build();
4145
}
4246

4347
@Test
@@ -159,4 +163,14 @@ public void testDeleteCustomer() throws Exception {
159163

160164
verify(customerService).deleteCustomerById(anyLong());
161165
}
166+
167+
@Test
168+
public void testNotFoundException() throws Exception {
169+
170+
when(customerService.getCustomerById(anyLong())).thenThrow(ResourceNotFoundException.class);
171+
172+
mockMvc.perform(get(CustomerController.BASE_URL + "/222")
173+
.contentType(MediaType.APPLICATION_JSON))
174+
.andExpect(status().isNotFound());
175+
}
162176
}

0 commit comments

Comments
 (0)