gpt4 book ai didi

java - 使用 DB 对 Hibernate 进行单元测试,测试返回 nullpointerException

转载 作者:太空宇宙 更新时间:2023-11-04 11:02:12 25 4
gpt4 key购买 nike

我用 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;

}

最佳答案

Should I wrote Junit tests in another way (different way to 'mock' that DB)

您没有提到您在生产中使用的数据库,但如果它是传统的基于 SQL 的数据库,那么您很可能可以使用 H2HSQL嵌入式数据库以其一种兼容性模式进行单元测试(如果您将嵌入式连接设置为默认 spring 配置文件的数据源,我发现这是最简单的方法)

关于java - 使用 DB 对 Hibernate 进行单元测试,测试返回 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46778265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com