/
ZufarFaiz
/
SberProject
Обзор
Документация
Войти
/
ZufarFaiz
/
SberProject
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/test/java/com/example/JobExchange_app/controllerTests/ApplicantProfileControllerTest.java
91 строка
4 KB
Семейный
Вынес некоторые методы в отдельные контроллеры
01 июн 2025, 23:26
01 июн 2025, 23:26
aafad78
Код
Авторство
О чём код?
package com.example.JobExchange_app.controllerTests; import com.example.JobExchange_app.controller.ApplicantProfileController; import com.example.JobExchange_app.dto.ApplicantProfileDto; import com.example.JobExchange_app.service.ApplicantService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @AutoConfigureMockMvc public class ApplicantProfileControllerTest { @Autowired private MockMvc mockMvc; @Mock private ApplicantService applicantServiceImpl; @InjectMocks private ApplicantProfileController controller; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } @Test void profile_ShouldReturnApplicantProfileView() throws Exception { String username = "testUser"; ApplicantProfileDto profileDto = new ApplicantProfileDto(); profileDto.setUserName("John Doe"); when(applicantServiceImpl.getUserName()).thenReturn(username); when(applicantServiceImpl.getProfile(username)).thenReturn(profileDto); mockMvc.perform(get("/applicant/profile/")) .andExpect(status().isOk()) .andExpect(view().name("applicantProfile")) .andExpect(model().attributeExists("profile")) .andExpect(model().attribute("profile", profileDto)); } @Test void editProfile_ShouldReturnUpdateView() throws Exception { String username = "testUser"; ApplicantProfileDto profileDto = new ApplicantProfileDto(); profileDto.setUserName("John Doe"); when(applicantServiceImpl.getUserName()).thenReturn(username); when(applicantServiceImpl.getProfile(username)).thenReturn(profileDto); mockMvc.perform(get("/applicant/profile/edit")) .andExpect(status().isOk()) .andExpect(view().name("updateApplicantProfile")) .andExpect(model().attributeExists("profile")) .andExpect(model().attribute("profile", profileDto)); } @Test void updateProfile_ShouldRedirectToProfile() throws Exception { String username = "testUser"; ApplicantProfileDto profileDto = new ApplicantProfileDto(); profileDto.setUserName("Updated Name"); when(applicantServiceImpl.getUserName()).thenReturn(username); doNothing().when(applicantServiceImpl).updateProfile(eq(username), any(ApplicantProfileDto.class)); mockMvc.perform(put("/applicant/profile") .flashAttr("applicantProfileDto", profileDto)) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/applicant/profile/")); verify(applicantServiceImpl).updateProfile(username, profileDto); } }