我用ORM(Hibernate)编写了Rest-Api,现在我想测试该服务逻辑。我正在尝试模拟数据库的实体,但我并没有取得惊人的结果。我的选择是和Mockito一起得到这个,但现在我对此有了怀疑。一般来说,我从我写的测试中得到NullPointerException。我应该用另一种方式写Junit测试(用不同的方式"模拟"数据库)还是我不理解Hibernate?
这是我的代码:测试等级
public class ItemServiceImplTest {
@Spy
private ItemServiceImplementation itemServImpl;
@Mock
private BasketRepository basketRepository;
@Mock
private ItemRepository itemRepository;
@Mock
private Basket basket;
@Spy
private Item item;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
itemServImpl = new ItemServiceImplementation();
itemServImpl.setItemRepository(itemRepository);
itemServImpl.setBasketRepository(basketRepository);
}
@Test
public void testShowBasket() {
Iterable<Basket> element = basketRepository.findAll();
when(itemServImpl.showBasket()).thenReturn(element);
Iterable<Basket> elementOne = itemServImpl.showBasket();
assertThat(elementOne, is(equalTo(element)));
}
@Test
public void testAddToBasketStringInt() {
Item a = new Item("A", 40, 70, 3);
Basket testObjOne = new Basket(3, 70, a);
when(itemServImpl.addToBasket("A", 3)).thenReturn((Iterable<Basket>) basketRepository.save(testObjOne));
Iterable<Basket> zzz = itemServImpl.addToBasket("A", 3);
assertThat(zzz, is(equalTo(testObjOne)));
}
型号
@Entity
public class Item {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
private int price;
private int specialPrice;
private int qtyToDiscount;
public Item(){};
public Item(String name, int price, int specialPrice, int qtyToDiscount) {
this.name = name;
this.price = price;
this.specialPrice = specialPrice;
this.qtyToDiscount = qtyToDiscount;
}
@Entity
public class Basket {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;
private int quantity;
private int cost;
@OneToOne
@JoinTable(name = "BASKET_ORDERS", joinColumns = { @JoinColumn(name = "ORDER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ID") })
private Item item;
public Basket() {
}
public Basket(int quantity, int cost,Item items) {
this.quantity = quantity;
this.cost = cost;
this.item = items;
}
服务
@Service("itemService")
@Transactional
public class ItemServiceImplementation implements ItemCRUDService, ItemCostService, ItemFindService {
public static final Logger logger = LoggerFactory.getLogger(ItemServiceImplementation.class);
@Autowired
public void setItemRepository(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
@Autowired
public void setBasketRepository(BasketRepository basketRepository) {
this.basketRepository = basketRepository;
}
private BasketRepository basketRepository;
private ItemRepository itemRepository;
public Iterable<Basket> showBasket() {
return basketRepository.findAll();
}
public Iterable<Basket> addToBasket(String name, int qty) {
if (basketRepository.exists(itemRepository.findOne(findItem(name).getId()).getId())==false) {
logger.info("add to empty list");
basketRepository.save(new Basket(qty, itemCost(name, qty), findItem(name)));
logger.info("adding");
}
modifyOrder(findItem(name).getId(), qty);
itemCost(name, qty);
logger.info("modify");
return showBasket();
}
public Item findItem(String name) {
for (Item item : itemRepository.findAll()) {
if (item.getName().equals(name)) {
return item;
}
}
return null;
}
public int itemCost(String name, int qty) {
int price = 0;
int modulo = qty % findItem(name).getSpecialPrice();
price = findItem(name).getPrice() * modulo
+ findItem(name).getSpecialPrice() * ((qty - modulo) / `findItem(name).getSpecialPrice());`
return price;
}
我应该用另一种方式编写Junit测试吗(与"mock"不同DB)
您没有提到您在生产中使用的数据库,但如果它是一个传统的基于SQL的数据库,那么您很可能可以在单元测试的兼容模式中使用H2或HSQL嵌入式数据库(如果您将嵌入式连接设置为默认spring配置文件的数据源,我发现这是最简单的方法)