Persistence layer test
✅ Production code
1
2
3
4
5
6
7
8
9
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
/**
* select *
* from product
* where selling_status IN ('SELLING', 'HOLD');
*/
List<Product> findAllBySellingStatusIn(List<ProductSellingStatus> sellingStatuses);
}
✅ Test code
- can choose between
@SpringBootTestand@DataJpaTest @DataJpaTestis faster, as it only loads JPA libraries@ActiveProfilesis for using only test code data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//@SpringBootTest
@DataJpaTest //faster than spring, only brings JPA libraries
@ActiveProfiles("test") //do not use data.sql for test
class ProductRepositoryTest {
@Autowired
private ProductRepository productRepository;
@DisplayName("Get products according to their selling status")
@Test
void findAllBySellingStatusIn(){
//given
Product product1 = Product.builder()
.productNumber("001")
.type(HANDMADE)
.sellingStatus(SELLING)
.name("americano")
.price(4000)
.build();
Product product2 = Product.builder()
.productNumber("002")
.type(HANDMADE)
.sellingStatus(HOLD)
.name("latte")
.price(4500)
.build();
Product product3 = Product.builder()
.productNumber("003")
.type(BAKERY)
.sellingStatus(STOP_SELLING)
.name("croissant")
.price(3500)
.build();
productRepository.saveAll(List.of(product1, product2, product3));
//when
List<Product> products = productRepository.findAllBySellingStatusIn(List.of(SELLING, HOLD));
//then
assertThat(products).hasSize(2)
.extracting("productNumber", "name", "sellingStatus")
.containsExactlyInAnyOrder(
tuple("001", "americano", SELLING),
tuple("002", "latte", HOLD)
);
}
}
- when testing a list
- 1️⃣ check size
- 2️⃣ use
extracting()andcontains()to see if it has the value you expect
✅
✅
✅
This post is licensed under CC BY 4.0 by the author.