package com.bijian.service;
import com.bijian.BaseMockTest;
import com.bijian.dao.OrderDao;
import com.bijian.dto.AddOrderRsp;
import com.bijian.entity.Book;
import com.bijian.entity.Order;
import com.bijian.entity.User;
import com.bijian.enums.ReturnCodeEnum;
import com.bijian.service.impl.OrderServiceImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class OrderServiceTest extends BaseMockTest {
@Autowired
@InjectMocks
private OrderServiceImpl orderServiceImpl;
@Mock
private OrderDao mockOrderDao;
@Mock
private BookService mockBookService;
@Mock
private UserService mockUserService;
@Before
public void before() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void test_addOrder_01_success() throws Exception {
Order order = new Order();
order.setId(1);
order.setUserId(2);
order.setBookId(2);
order.setBookNum(2);
String mockUserName = "lisi";
String mockBookName = "javaScript";
BigDecimal mockBookPrice = new BigDecimal("10.2");
User mockUser = new User();
mockUser.setName(mockUserName);
when(mockUserService.findUserById(order.getUserId())).thenReturn(mockUser);
Book mockBook = new Book();
mockBook.setName(mockBookName);
mockBook.setPrice(mockBookPrice);
mockBook.setRemainNum(100);
when(mockBookService.findBookById(order.getBookId())).thenReturn(mockBook);
System.out.println(mockBook.getName());
when(mockOrderDao.addOrder(order)).thenReturn(1);
AddOrderRsp result = orderServiceImpl.addOrder(order);
verify(mockBookService, times(1)).updateBook(Mockito.any(Book.class));
Assert.assertEquals(ReturnCodeEnum.SUCCESS.getCode(), result.getRetCode());
}
@Test
public void test_addOrder_success() throws Exception {
Order order = new Order();
order.setId(1);
order.setUserId(2);
order.setBookId(2);
order.setBookNum(2);
String mockUserName = "lisi";
String mockBookName = "javaScript";
BigDecimal mockBookPrice = new BigDecimal("10.2");
User mockUser = new User();
mockUser.setName(mockUserName);
when(mockUserService.findUserById(Mockito.any(Integer.class))).thenReturn(mockUser);
Book mockBook = new Book();
mockBook.setName(mockBookName);
mockBook.setPrice(mockBookPrice);
mockBook.setRemainNum(100);
when(mockBookService.findBookById(Mockito.any(Integer.class))).thenReturn(mockBook);
when(mockOrderDao.addOrder(Mockito.any(Order.class))).thenAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invo) {
Object[] args = invo.getArguments();
Order reqOrder = (Order)args[0];
Assert.assertEquals(mockBookName, reqOrder.getBookName());
Assert.assertEquals(mockUserName, reqOrder.getUserName());
Assert.assertEquals(mockBookPrice, reqOrder.getBookPrice());
return 1;
}
});
AddOrderRsp result = orderServiceImpl.addOrder(order);
verify(mockBookService, times(1)).updateBook(Mockito.any(Book.class));
Assert.assertEquals(ReturnCodeEnum.SUCCESS.getCode(), result.getRetCode());
}
}