Skip to content

Commit 0446d41

Browse files
committed
10_10_not_found_422
1 parent cc129ef commit 0446d41

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎src/main/java/ru/javawebinar/topjava/util/exception/NotFoundException.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package ru.javawebinar.topjava.util.exception;
22

3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
// http://stackoverflow.com/a/22358422/548473
7+
@ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY, reason = "No data found") // 422
38
public class NotFoundException extends RuntimeException {
49
public NotFoundException(String message) {
510
super(message);

‎src/test/java/ru/javawebinar/topjava/web/meal/MealRestControllerTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ void getUnauth() throws Exception {
4646
.andExpect(status().isUnauthorized());
4747
}
4848

49+
@Test
50+
void getNotFound() throws Exception {
51+
perform(MockMvcRequestBuilders.get(REST_URL + ADMIN_MEAL_ID)
52+
.with(userHttpBasic(user)))
53+
.andDo(print())
54+
.andExpect(status().isUnprocessableEntity());
55+
}
56+
4957
@Test
5058
void delete() throws Exception {
5159
perform(MockMvcRequestBuilders.delete(REST_URL + MEAL1_ID)
@@ -54,6 +62,13 @@ void delete() throws Exception {
5462
assertThrows(NotFoundException.class, () -> mealService.get(MEAL1_ID, USER_ID));
5563
}
5664

65+
@Test
66+
void deleteNotFound() throws Exception {
67+
perform(MockMvcRequestBuilders.delete(REST_URL + ADMIN_MEAL_ID)
68+
.with(userHttpBasic(user)))
69+
.andExpect(status().isUnprocessableEntity());
70+
}
71+
5772
@Test
5873
void update() throws Exception {
5974
Meal updated = getUpdated();

‎src/test/java/ru/javawebinar/topjava/web/user/AdminRestControllerTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.http.MediaType;
66
import org.springframework.test.web.servlet.ResultActions;
77
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
8+
import ru.javawebinar.topjava.UserTestData;
89
import ru.javawebinar.topjava.model.User;
910
import ru.javawebinar.topjava.service.UserService;
1011
import ru.javawebinar.topjava.util.exception.NotFoundException;
@@ -37,6 +38,14 @@ void get() throws Exception {
3738
.andExpect(USER_MATCHER.contentJson(admin));
3839
}
3940

41+
@Test
42+
void getNotFound() throws Exception {
43+
perform(MockMvcRequestBuilders.get(REST_URL + NOT_FOUND)
44+
.with(userHttpBasic(admin)))
45+
.andDo(print())
46+
.andExpect(status().isUnprocessableEntity());
47+
}
48+
4049
@Test
4150
void getByEmail() throws Exception {
4251
perform(MockMvcRequestBuilders.get(REST_URL + "by-email?email=" + user.getEmail())
@@ -55,6 +64,14 @@ void delete() throws Exception {
5564
assertThrows(NotFoundException.class, () -> userService.get(USER_ID));
5665
}
5766

67+
@Test
68+
void deleteNotFound() throws Exception {
69+
perform(MockMvcRequestBuilders.delete(REST_URL + NOT_FOUND)
70+
.with(userHttpBasic(admin)))
71+
.andDo(print())
72+
.andExpect(status().isUnprocessableEntity());
73+
}
74+
5875
@Test
5976
void getUnAuth() throws Exception {
6077
perform(MockMvcRequestBuilders.get(REST_URL))

0 commit comments

Comments
 (0)