/
Renonr
/
SpringProject
Обзор
Документация
Войти
/
Renonr
/
SpringProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
Spring_Branch
src/test/java/com/example/onlineshop/OrderControllerTest.java
54 строки
2 KB
Renonr
"Version with DTO"
19 фев 2025, 03:07
19 фев 2025, 03:07
37e3ebe
Код
Авторство
О чём код?
package com.example.onlineshop; import com.example.onlineshop.controllers.OrderController; import com.example.onlineshop.dto.OrderDTO; import com.example.onlineshop.services.OrderService; 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.ResponseEntity; import org.springframework.security.core.Authentication; import java.util.Arrays; import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; class OrderControllerTest { @Mock private OrderService orderService; @Mock private Authentication authentication; @InjectMocks private OrderController orderController; @BeforeEach void setUp() { MockitoAnnotations.initMocks(this); when(authentication.getName()).thenReturn("test@example.com"); } @Test void placeOrder_ShouldReturnOrderId() { OrderDTO order = new OrderDTO(); when(orderService.placeOrder(anyString())).thenReturn(order); ResponseEntity<?> response = orderController.placeOrder(authentication); assertEquals("Заказ успешно оформлен", ((Map<?, ?>) response.getBody()).get("message")); } @Test void getOrders_ShouldReturnOrders() { List<OrderDTO> orders = Arrays.asList(new OrderDTO(), new OrderDTO()); when(orderService.getUserOrders(anyString())).thenReturn(orders); List<OrderDTO> response = orderController.getOrders(authentication); assertEquals(2, response.size()); } }