Advertisement
shchuko

JUnit5

Nov 2nd, 2021
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. package com.itmo.microservices.shop.catalog.repository;
  2.  
  3. import com.itmo.microservices.shop.catalog.HardcodedValues;
  4. import com.itmo.microservices.shop.catalog.impl.repository.ItemRepository;
  5. import org.junit.jupiter.api.Test;
  6. import org.junit.jupiter.api.extension.ExtendWith;
  7. import org.mockito.Mockito;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
  10. import org.springframework.jdbc.core.JdbcTemplate;
  11. import org.springframework.test.context.ActiveProfiles;
  12. import org.springframework.test.context.junit.jupiter.SpringExtension;
  13.  
  14. import javax.persistence.EntityManager;
  15. import javax.sql.DataSource;
  16.  
  17. import java.util.stream.Collectors;
  18.  
  19. import org.junit.jupiter.api.Assertions;
  20.  
  21. @ActiveProfiles("dev")
  22. @ExtendWith(SpringExtension.class)
  23. @DataJpaTest
  24. public class ItemRepositoryTest extends HardcodedValues {
  25.     @Autowired
  26.     private DataSource dataSource;
  27.     @Autowired
  28.     private JdbcTemplate jdbcTemplate;
  29.     @Autowired
  30.     private EntityManager entityManager;
  31.     @Autowired
  32.     private ItemRepository itemRepository;
  33.  
  34.     @Test
  35.     public void injectedComponentsAreNotNull(){
  36.         Assertions.assertNotNull(dataSource);
  37.         Assertions.assertNotNull(jdbcTemplate);
  38.         Assertions.assertNotNull(entityManager);
  39.         Assertions.assertNotNull(itemRepository);
  40.     }
  41.  
  42.     @Test
  43.     public void returnAvailableItems(){
  44.         itemRepository.saveAllAndFlush(mockedItems);
  45.  
  46.         var test = itemRepository.findAllByAmountGreaterThan(0);
  47.         var correct = mockedItems.stream().filter(item -> item.getAmount() > 0).collect(Collectors.toList());
  48.  
  49.         test.forEach(t -> t.setId(null));
  50.         correct.forEach(t -> t.setId(null));
  51.  
  52.         Assertions.assertEquals(correct, test);
  53.     }
  54.  
  55.     @Test
  56.     public void whenSave_thenGenerateNewUUID(){
  57.         itemRepository.save(mockedItem);
  58.  
  59.         var item = itemRepository.findAll().stream().findFirst();
  60.         var test = item.get().getId();
  61.  
  62.         var correct = mockedItem.getId();
  63.  
  64.         Assertions.assertNotEquals(correct, test);
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement