|
| 1 | +package guru.springframework.repositories; |
| 2 | + |
| 3 | +import guru.springframework.configuration.RepositoryConfiguration; |
| 4 | +import guru.springframework.domain.Product; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 10 | + |
| 11 | +import java.math.BigDecimal; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNotNull; |
| 15 | +import static org.junit.Assert.assertNull; |
| 16 | + |
| 17 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 18 | +@SpringApplicationConfiguration(classes = {RepositoryConfiguration.class}) |
| 19 | +public class ProductRepositoryTest { |
| 20 | + |
| 21 | + private ProductRepository productRepository; |
| 22 | + |
| 23 | + @Autowired |
| 24 | + public void setProductRepository(ProductRepository productRepository) { |
| 25 | + this.productRepository = productRepository; |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testSaveProduct(){ |
| 30 | + //setup product |
| 31 | + Product product = new Product(); |
| 32 | + product.setDescription("Spring Framework Guru Shirt"); |
| 33 | + product.setPrice(new BigDecimal("18.95")); |
| 34 | + product.setProductId("1234"); |
| 35 | + |
| 36 | + //save product, verify has ID value after save |
| 37 | + assertNull(product.getId()); //null before save |
| 38 | + productRepository.save(product); |
| 39 | + assertNotNull(product.getId()); //not null after save |
| 40 | + |
| 41 | + //fetch from DB |
| 42 | + Product fetchedProduct = productRepository.findOne(product.getId()); |
| 43 | + |
| 44 | + //should not be null |
| 45 | + assertNotNull(fetchedProduct); |
| 46 | + |
| 47 | + //should equal |
| 48 | + assertEquals(product.getId(), fetchedProduct.getId()); |
| 49 | + assertEquals(product.getDescription(), fetchedProduct.getDescription()); |
| 50 | + |
| 51 | + //update description and save |
| 52 | + fetchedProduct.setDescription("New Description"); |
| 53 | + productRepository.save(fetchedProduct); |
| 54 | + |
| 55 | + //get from DB, should be updated |
| 56 | + Product fetchedUpdatedProduct = productRepository.findOne(fetchedProduct.getId()); |
| 57 | + assertEquals(fetchedProduct.getDescription(), fetchedUpdatedProduct.getDescription()); |
| 58 | + |
| 59 | + //verify count of products in DB |
| 60 | + long productCount = productRepository.count(); |
| 61 | + assertEquals(productCount, 1); |
| 62 | + |
| 63 | + //get all products, list should only have one |
| 64 | + Iterable<Product> products = productRepository.findAll(); |
| 65 | + |
| 66 | + int count = 0; |
| 67 | + |
| 68 | + for(Product p : products){ |
| 69 | + count++; |
| 70 | + } |
| 71 | + |
| 72 | + assertEquals(count, 1); |
| 73 | + } |
| 74 | +} |
0 commit comments