/
ZufarFaiz
/
SberProject
Обзор
Документация
Войти
/
ZufarFaiz
/
SberProject
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/test/java/com/example/JobExchange_app/controllerTests/JobResponseControllerTest.java
87 строк
3 KB
Семейный
Написал тесты к контроллерам, сервисам. Пока только 47%. Дополню позже
01 июн 2025, 23:34
01 июн 2025, 23:34
dd92030
Код
Авторство
О чём код?
package com.example.JobExchange_app.controllerTests; import com.example.JobExchange_app.controller.JobResponseController; import com.example.JobExchange_app.dto.ApplicantProfileDto; import com.example.JobExchange_app.service.JobResponseService; import com.example.JobExchange_app.service.UserNameService; 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.hamcrest.Matchers.hasSize; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import java.util.ArrayList; import java.util.List; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @AutoConfigureMockMvc public class JobResponseControllerTest { @Autowired private MockMvc mockMvc; @Mock private JobResponseService jobResponseServiceImpl; @Mock private UserNameService applicantServiceImpl; @InjectMocks private JobResponseController controller; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } @Test void viewApplicants_ShouldReturnViewWithApplicantsList() throws Exception { Long jobId = 1L; String username = "testUser"; List<ApplicantProfileDto> applicants =new ArrayList<>(); ApplicantProfileDto applicantProfileDto1=new ApplicantProfileDto(); ApplicantProfileDto applicantProfileDto2 =new ApplicantProfileDto(); applicantProfileDto1.setUserName("John Doe"); applicantProfileDto1.setEmail("john@example.com"); applicantProfileDto2.setUserName("Jane Smith"); applicantProfileDto2.setEmail("jane@example.com"); applicants.add(applicantProfileDto1); applicants.add(applicantProfileDto2); when(jobResponseServiceImpl.viewApplicants(jobId)).thenReturn(applicants); mockMvc.perform(get("/responses/applicants/{id}", jobId)) .andExpect(status().isOk()) .andExpect(view().name("viewApplicants")) .andExpect(model().attributeExists("applicantProfileDtos")) .andExpect(model().attribute("applicantProfileDtos", hasSize(2))) .andExpect(model().attribute("applicantProfileDtos", applicants)); } @Test void respondToVacancy_ShouldCallServiceAndRedirect() throws Exception { Long vacancyId = 1L; String username = "applicant123"; when(applicantServiceImpl.getUserName()).thenReturn(username); doNothing().when(jobResponseServiceImpl).respondToVacancy(username, vacancyId); mockMvc.perform(post("/responses/response") .param("vacancyId", vacancyId.toString())) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/applicant/profile/")); verify(jobResponseServiceImpl).respondToVacancy(username, vacancyId); } }