Skip to content

Commit

Permalink
OrderService unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizerpwn committed May 10, 2024
1 parent 011c44f commit 7267ea4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.ibrahimokic.ordermanagement.service.impl;

import com.ibrahimokic.ordermanagement.domain.dto.AddressDto;
import com.ibrahimokic.ordermanagement.domain.dto.OrderItemDto;
import com.ibrahimokic.ordermanagement.domain.entity.*;
import com.ibrahimokic.ordermanagement.domain.dto.OrderDto;
import com.ibrahimokic.ordermanagement.mapper.Mapper;
import com.ibrahimokic.ordermanagement.mapper.impl.AddressMapperImpl;
import com.ibrahimokic.ordermanagement.mapper.impl.OrderItemMapperImpl;
import com.ibrahimokic.ordermanagement.mapper.impl.OrderMapperImpl;
import com.ibrahimokic.ordermanagement.repository.*;
import com.ibrahimokic.ordermanagement.service.OrderService;
import com.ibrahimokic.ordermanagement.utils.Utils;
Expand All @@ -25,9 +26,9 @@ public class OrderServiceImpl implements OrderService {
private final OrderRepository orderRepository;
private final OrderItemRepository orderItemRepository;
private final UserRepository userRepository;
private final Mapper<Order, OrderDto> orderMapper;
private final Mapper<Address, AddressDto> addressMapper;
private final Mapper<OrderItem, OrderItemDto> orderItemMapper;
private final OrderMapperImpl orderMapper;
private final AddressMapperImpl addressMapper;
private final OrderItemMapperImpl orderItemMapper;
private final AddressRepository addressRepository;
private final ProductRepository productRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.ibrahimokic.ordermanagement.service;

import com.ibrahimokic.ordermanagement.domain.dto.OrderDto;
import com.ibrahimokic.ordermanagement.domain.entity.Order;
import com.ibrahimokic.ordermanagement.mapper.impl.OrderMapperImpl;
import com.ibrahimokic.ordermanagement.repository.*;
import com.ibrahimokic.ordermanagement.service.impl.OrderServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class OrderServiceImplTest {
@Mock
private OrderRepository orderRepository;

@Mock
private OrderMapperImpl orderMapper;

@InjectMocks
private OrderServiceImpl orderService;

@Test
void testGetAllOrdersWithDetails() {
Order order = new Order();
List<Order> orders = new ArrayList<>();
orders.add(order);

when(orderRepository.findAll()).thenReturn(orders);
when(orderMapper.mapTo(order)).thenReturn(new OrderDto());

List<OrderDto> result = orderService.getAllOrdersWithDetails();

assertEquals(1, result.size());
verify(orderRepository, times(1)).findAll();
verify(orderMapper, times(1)).mapTo(order);
}

@Test
void testGetAllOrders() {
Order order = new Order();
List<Order> orders = new ArrayList<>();
orders.add(order);

when(orderRepository.findAll()).thenReturn(orders);

List<Order> result = orderService.getAllOrders();

assertEquals(1, result.size());
verify(orderRepository, times(1)).findAll();
}

@Test
void testGetOrderById() {
Long orderId = 1L;
Order order = new Order();

when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
when(orderMapper.mapTo(order)).thenReturn(new OrderDto());

OrderDto result = orderService.getOrderById(orderId);

assertNotNull(result);
verify(orderRepository, times(1)).findById(orderId);
verify(orderMapper, times(1)).mapTo(order);
}

@Test
void testDeleteOrderById() {
Long orderId = 1L;
Order order = new Order();

when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));

boolean result = orderService.deleteOrderById(orderId);

assertTrue(result);
verify(orderRepository, times(1)).findById(orderId);
verify(orderRepository, times(1)).deleteById(orderId);
}
}

0 comments on commit 7267ea4

Please sign in to comment.