Skip to content

Commit

Permalink
fix: test id variables
Browse files Browse the repository at this point in the history
id로 불러오는 api들 상수가 아닌 test용으로 생성한 객체의 id를 쓰도록 변경
  • Loading branch information
Village-GG-Water committed Nov 3, 2024
1 parent b8d2b22 commit 1fd95c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/test/java/com/kert/AdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ public void getAllAdminsWithAdmin() throws Exception {
@Test
@DisplayName("get admin by id with not_admin")
public void getAdminByIdWithNotAdmin() throws Exception {
when(adminService.getAdminByStudentId(1L)).thenReturn(testAdmin);
when(adminService.getAdminByStudentId(testAdmin.getStudentId())).thenReturn(testAdmin);

mockMvc.perform(get("/admin/1")).andExpect(status().isForbidden());
mockMvc.perform(get("/admin/{studentId}", testAdmin.getStudentId())).andExpect(status().isForbidden());
}

@Test
@DisplayName("get admin by id with not_admin")
@WithMockUser(roles = "ADMIN")
public void getAdminByIdWithAdmin() throws Exception {
when(adminService.getAdminByStudentId(1L)).thenReturn(testAdmin);
when(adminService.getAdminByStudentId(testAdmin.getStudentId())).thenReturn(testAdmin);

mockMvc.perform(get("/admin/1")).andExpect(status().isOk())
mockMvc.perform(get("/admin/{studentId}", testAdmin.getStudentId())).andExpect(status().isOk())
.andExpect(jsonPath("$.student_id").value(testAdmin.getStudentId()))
.andExpect(jsonPath("$.generation").value(testAdmin.getGeneration()))
.andExpect(jsonPath("$.role").value(testAdmin.getRole()))
Expand Down Expand Up @@ -128,18 +128,18 @@ public void createAdminWithAdmin() throws Exception {
@Test
@DisplayName("update admin with not_admin")
public void updateAdminWithNotAdmin() throws Exception {
when(adminService.updateAdmin(1L, testAdmin)).thenReturn(testAdmin);
when(adminService.updateAdmin(testAdmin.getStudentId(), testAdmin)).thenReturn(testAdmin);

mockMvc.perform(put("/admin/1").contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isForbidden());
mockMvc.perform(put("/admin/{studentId}", testAdmin.getStudentId()).contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isForbidden());
}

@Test
@DisplayName("update admin with admin")
@WithMockUser(roles = "ADMIN")
public void updateAdminWithAdmin() throws Exception {
when(adminService.updateAdmin(1L, testAdmin)).thenReturn(testAdmin);
when(adminService.updateAdmin(testAdmin.getStudentId(), testAdmin)).thenReturn(testAdmin);

mockMvc.perform(put("/admin/1").contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isOk())
mockMvc.perform(put("/admin/{studentId}", testAdmin.getStudentId()).contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isOk())
.andExpect(jsonPath("$.student_id").value(testAdmin.getStudentId()))
.andExpect(jsonPath("$.generation").value(testAdmin.getGeneration()))
.andExpect(jsonPath("$.role").value(testAdmin.getRole()))
Expand All @@ -149,13 +149,13 @@ public void updateAdminWithAdmin() throws Exception {
@Test
@DisplayName("delete admin with not_admin")
public void deleteAdminWithNotAdmin() throws Exception {
mockMvc.perform(delete("/admin/1")).andExpect(status().isForbidden());
mockMvc.perform(delete("/admin/{studentId}", testAdmin.getStudentId())).andExpect(status().isForbidden());
}

@Test
@DisplayName("delete admin with not_admin")
@WithMockUser(roles = "ADMIN")
public void deleteAdminWithAdmin() throws Exception {
mockMvc.perform(delete("/admin/1")).andExpect(status().isNoContent());
mockMvc.perform(delete("/admin/{studentId}", testAdmin.getStudentId())).andExpect(status().isNoContent());
}
}
17 changes: 9 additions & 8 deletions src/test/java/com/kert/HistoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class HistoryTest {
private String testRequestBody;

private final History testHistory = new History();
private final Long testHistoryId = 1L;

@BeforeAll
public void setUp() {
Expand Down Expand Up @@ -76,9 +77,9 @@ public void getAllHistories() throws Exception {
@Test
@DisplayName("get history by id")
public void getHistoryById() throws Exception {
when(historyService.getHistoryById(1L)).thenReturn(testHistory);
when(historyService.getHistoryById(testHistoryId)).thenReturn(testHistory);

mockMvc.perform(get("/histories/1")).andExpect(status().isOk())
mockMvc.perform(get("/histories/{historyId}", testHistoryId)).andExpect(status().isOk())
.andExpect(jsonPath("$.year").value(testHistory.getYear()))
.andExpect(jsonPath("$.month").value(testHistory.getMonth()))
.andExpect(jsonPath("$.content").value(testHistory.getContent()));
Expand Down Expand Up @@ -107,18 +108,18 @@ public void createHistoryWithAdmin() throws Exception {
@Test
@DisplayName("update history with not_admin")
public void updateHistoryWithNotAdmin() throws Exception {
when(historyService.updateHistory(1L, testHistory)).thenReturn(testHistory);
when(historyService.updateHistory(testHistoryId, testHistory)).thenReturn(testHistory);

mockMvc.perform(put("/histories/1").contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isForbidden());
mockMvc.perform(put("/histories/{historyId}", testHistoryId).contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isForbidden());
}

@Test
@DisplayName("update history with admin")
@WithMockUser(roles = "ADMIN")
public void updateHistoryWithAdmin() throws Exception {
when(historyService.updateHistory(1L, testHistory)).thenReturn(testHistory);
when(historyService.updateHistory(testHistoryId, testHistory)).thenReturn(testHistory);

mockMvc.perform(put("/histories/1").contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isOk())
mockMvc.perform(put("/histories/{historyId}", testHistoryId).contentType(MediaType.APPLICATION_JSON).content(testRequestBody)).andExpect(status().isOk())
.andExpect(jsonPath("$.year").value(testHistory.getYear()))
.andExpect(jsonPath("$.month").value(testHistory.getMonth()))
.andExpect(jsonPath("$.content").value(testHistory.getContent()));
Expand All @@ -127,13 +128,13 @@ public void updateHistoryWithAdmin() throws Exception {
@Test
@DisplayName("delete history with not_admin")
public void deleteHistoryWithNotAdmin() throws Exception {
mockMvc.perform(delete("/histories/1")).andExpect(status().isForbidden());
mockMvc.perform(delete("/histories/{historyId}", testHistoryId)).andExpect(status().isForbidden());
}

@Test
@DisplayName("delete history with admin")
@WithMockUser(roles = "ADMIN")
public void deleteHistoryWithAdmin() throws Exception {
mockMvc.perform(delete("/histories/1")).andExpect(status().isNoContent());
mockMvc.perform(delete("/histories/{historyId}", testHistoryId)).andExpect(status().isNoContent());
}
}

0 comments on commit 1fd95c0

Please sign in to comment.