Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.itmo.microservices.shop.catalog.repository;
- import com.itmo.microservices.shop.catalog.HardcodedValues;
- import com.itmo.microservices.shop.catalog.impl.repository.ItemRepository;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.extension.ExtendWith;
- import org.mockito.Mockito;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.test.context.ActiveProfiles;
- import org.springframework.test.context.junit.jupiter.SpringExtension;
- import javax.persistence.EntityManager;
- import javax.sql.DataSource;
- import java.util.stream.Collectors;
- import org.junit.jupiter.api.Assertions;
- @ActiveProfiles("dev")
- @ExtendWith(SpringExtension.class)
- @DataJpaTest
- public class ItemRepositoryTest extends HardcodedValues {
- @Autowired
- private DataSource dataSource;
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @Autowired
- private EntityManager entityManager;
- @Autowired
- private ItemRepository itemRepository;
- @Test
- public void injectedComponentsAreNotNull(){
- Assertions.assertNotNull(dataSource);
- Assertions.assertNotNull(jdbcTemplate);
- Assertions.assertNotNull(entityManager);
- Assertions.assertNotNull(itemRepository);
- }
- @Test
- public void returnAvailableItems(){
- itemRepository.saveAllAndFlush(mockedItems);
- var test = itemRepository.findAllByAmountGreaterThan(0);
- var correct = mockedItems.stream().filter(item -> item.getAmount() > 0).collect(Collectors.toList());
- test.forEach(t -> t.setId(null));
- correct.forEach(t -> t.setId(null));
- Assertions.assertEquals(correct, test);
- }
- @Test
- public void whenSave_thenGenerateNewUUID(){
- itemRepository.save(mockedItem);
- var item = itemRepository.findAll().stream().findFirst();
- var test = item.get().getId();
- var correct = mockedItem.getId();
- Assertions.assertNotEquals(correct, test);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement