/
Ozzus
/
SciTech
Обзор
Документация
Войти
/
Ozzus
/
SciTech
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/org/example/scitech/SciTechApplicationTests.java
322 строки
11 KB
Ozzus
добавлены CRUD,тесты написаны,swagger работает,НАЙК!
05 апр 2025, 15:14
05 апр 2025, 15:14
67d8148
Код
Авторство
О чём код?
package org.example.scitech; import org.example.scitech.controller.UniversalController; import org.example.scitech.models.Client; import org.example.scitech.models.ClientAddresses; import org.example.scitech.models.Role; import org.example.scitech.models.Status; import org.example.scitech.repositories.ClientAddressesRepository; import org.example.scitech.repositories.ClientRepository; import org.example.scitech.repositories.RoleRepository; import org.example.scitech.repositories.StatusRepository; 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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Arrays; import java.util.List; import java.util.Optional; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; class SciTechApplicationTests { private MockMvc mockMvc; @Mock private ClientRepository clientRepository; @Mock private ClientAddressesRepository clientAddressesRepository; @Mock private RoleRepository roleRepository; @Mock private StatusRepository statusRepository; @InjectMocks private UniversalController universalController; private final ObjectMapper objectMapper = new ObjectMapper(); @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); this.mockMvc = MockMvcBuilders.standaloneSetup(universalController).build(); } // Тесты для Client @Test void testCreateClient() throws Exception { Client client = new Client(); client.setClientName("Test Client"); when(clientRepository.save(any(Client.class))).thenReturn(client); mockMvc.perform(post("/api/clients") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(client))) .andExpect(status().isOk()) .andExpect(jsonPath("$.clientName").value("Test Client")); } @Test void testGetAllClients() throws Exception { Client client1 = new Client(); client1.setClientName("Client 1"); Client client2 = new Client(); client2.setClientName("Client 2"); when(clientRepository.findAll()).thenReturn(Arrays.asList(client1, client2)); mockMvc.perform(get("/api/clients")) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()").value(2)); } @Test void testGetClientById() throws Exception { Client client = new Client(); client.setClientName("Test Client"); when(clientRepository.findById(1L)).thenReturn(Optional.of(client)); mockMvc.perform(get("/api/clients/1")) .andExpect(status().isOk()) .andExpect(jsonPath("$.clientName").value("Test Client")); } @Test void testUpdateClient() throws Exception { Client client = new Client(); client.setClientName("Updated Client"); when(clientRepository.findById(1L)).thenReturn(Optional.of(client)); when(clientRepository.save(any(Client.class))).thenReturn(client); mockMvc.perform(put("/api/clients/1") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(client))) .andExpect(status().isOk()) .andExpect(jsonPath("$.clientName").value("Updated Client")); } @Test void testDeleteClient() throws Exception { Client client = new Client(); when(clientRepository.findById(1L)).thenReturn(Optional.of(client)); mockMvc.perform(delete("/api/clients/1")) .andExpect(status().isOk()); } // Тесты для ClientAddresses @Test void testCreateClientAddress() throws Exception { ClientAddresses clientAddress = new ClientAddresses(); clientAddress.setCity("Test City"); when(clientAddressesRepository.save(any(ClientAddresses.class))).thenReturn(clientAddress); mockMvc.perform(post("/api/client-addresses") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(clientAddress))) .andExpect(status().isOk()) .andExpect(jsonPath("$.city").value("Test City")); } @Test void testGetAllClientAddresses() throws Exception { ClientAddresses address1 = new ClientAddresses(); address1.setCity("City 1"); ClientAddresses address2 = new ClientAddresses(); address2.setCity("City 2"); when(clientAddressesRepository.findAll()).thenReturn(Arrays.asList(address1, address2)); mockMvc.perform(get("/api/client-addresses")) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()").value(2)); } @Test void testGetClientAddressById() throws Exception { ClientAddresses clientAddress = new ClientAddresses(); clientAddress.setCity("Test City"); when(clientAddressesRepository.findById(1L)).thenReturn(Optional.of(clientAddress)); mockMvc.perform(get("/api/client-addresses/1")) .andExpect(status().isOk()) .andExpect(jsonPath("$.city").value("Test City")); } @Test void testUpdateClientAddress() throws Exception { ClientAddresses clientAddress = new ClientAddresses(); clientAddress.setCity("Updated City"); when(clientAddressesRepository.findById(1L)).thenReturn(Optional.of(clientAddress)); when(clientAddressesRepository.save(any(ClientAddresses.class))).thenReturn(clientAddress); mockMvc.perform(put("/api/client-addresses/1") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(clientAddress))) .andExpect(status().isOk()) .andExpect(jsonPath("$.city").value("Updated City")); } @Test void testDeleteClientAddress() throws Exception { ClientAddresses clientAddress = new ClientAddresses(); when(clientAddressesRepository.findById(1L)).thenReturn(Optional.of(clientAddress)); mockMvc.perform(delete("/api/client-addresses/1")) .andExpect(status().isOk()); } // Тесты для Role @Test void testCreateRole() throws Exception { Role role = new Role(); role.setName("Test Role"); when(roleRepository.save(any(Role.class))).thenReturn(role); mockMvc.perform(post("/api/roles") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(role))) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("Test Role")); } @Test void testGetAllRoles() throws Exception { Role role1 = new Role(); role1.setName("Role 1"); Role role2 = new Role(); role2.setName("Role 2"); when(roleRepository.findAll()).thenReturn(Arrays.asList(role1, role2)); mockMvc.perform(get("/api/roles")) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()").value(2)); } @Test void testGetRoleById() throws Exception { Role role = new Role(); role.setName("Test Role"); when(roleRepository.findById(1L)).thenReturn(Optional.of(role)); mockMvc.perform(get("/api/roles/1")) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("Test Role")); } @Test void testUpdateRole() throws Exception { Role role = new Role(); role.setName("Updated Role"); when(roleRepository.findById(1L)).thenReturn(Optional.of(role)); when(roleRepository.save(any(Role.class))).thenReturn(role); mockMvc.perform(put("/api/roles/1") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(role))) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("Updated Role")); } @Test void testDeleteRole() throws Exception { Role role = new Role(); when(roleRepository.findById(1L)).thenReturn(Optional.of(role)); mockMvc.perform(delete("/api/roles/1")) .andExpect(status().isOk()); } // Тесты для Status @Test void testCreateStatus() throws Exception { Status status = new Status(); status.setName("Test Status"); when(statusRepository.save(any(Status.class))).thenReturn(status); mockMvc.perform(post("/api/statuses") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(status))) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("Test Status")); } @Test void testGetAllStatuses() throws Exception { Status status1 = new Status(); status1.setName("Status 1"); Status status2 = new Status(); status2.setName("Status 2"); when(statusRepository.findAll()).thenReturn(Arrays.asList(status1, status2)); mockMvc.perform(get("/api/statuses")) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()").value(2)); } @Test void testGetStatusById() throws Exception { Status status = new Status(); status.setName("Test Status"); when(statusRepository.findById(1L)).thenReturn(Optional.of(status)); mockMvc.perform(get("/api/statuses/1")) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("Test Status")); } @Test void testUpdateStatus() throws Exception { Status status = new Status(); status.setName("Updated Status"); when(statusRepository.findById(1L)).thenReturn(Optional.of(status)); when(statusRepository.save(any(Status.class))).thenReturn(status); mockMvc.perform(put("/api/statuses/1") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(status))) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("Updated Status")); } @Test void testDeleteStatus() throws Exception { Status status = new Status(); when(statusRepository.findById(1L)).thenReturn(Optional.of(status)); mockMvc.perform(delete("/api/statuses/1")) .andExpect(status().isOk()); } }