java 50 lines · 8 steps

Testing a JPA repository against real Postgres in Spring

A @DataJpaTest slice runs repository queries against a throwaway Postgres container instead of an in-memory database.

Explained by highlit
1@DataJpaTest
2@Testcontainers
3@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
4class OrderRepositoryIT {
5 
6 @Container
7 static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine")
8 .withDatabaseName("shop")
9 .withUsername("shop")
10 .withPassword("secret");
11 
12 @DynamicPropertySource
13 static void datasourceProps(DynamicPropertyRegistry registry) {
14 registry.add("spring.datasource.url", postgres::getJdbcUrl);
15 registry.add("spring.datasource.username", postgres::getUsername);
16 registry.add("spring.datasource.password", postgres::getPassword);
17 registry.add("spring.jpa.hibernate.ddl-auto", () -> "create-drop");
18 }
19 
20 @Autowired
21 private TestEntityManager em;
22 
23 @Autowired
24 private OrderRepository orders;
25 
26 private Customer alice;
27 
28 @BeforeEach
29 void seed() {
30 alice = em.persist(new Customer("alice@example.com", "Alice"));
31 Customer bob = em.persist(new Customer("bob@example.com", "Bob"));
32 
33 em.persist(new Order(alice, new BigDecimal("49.90"), OrderStatus.SHIPPED));
34 em.persist(new Order(alice, new BigDecimal("12.00"), OrderStatus.PENDING));
35 em.persist(new Order(bob, new BigDecimal("99.99"), OrderStatus.SHIPPED));
36 
37 em.flush();
38 em.clear();
39 }
40 
41 @Test
42 void findsPendingOrdersForCustomer() {
43 List<Order> pending = orders.findByCustomerAndStatus(alice, OrderStatus.PENDING);
44 
45 assertThat(pending).hasSize(1)
46 .first()
47 .extracting(Order::getTotal)
48 .isEqualTo(new BigDecimal("12.00"));
49 }
50}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Testing repositories against the real database engine catches dialect-specific bugs an in-memory substitute would hide.
  2. 2@DynamicPropertySource wires runtime container details into Spring config before the context starts.
  3. 3Flushing and clearing the persistence context forces queries to hit the database rather than the entity cache.

Related explainers

java
@Component
@Converter
public class EncryptedStringConverter implements AttributeConverter<String, String> {
 

Transparent column encryption in Spring & JPA

encryption aes-gcm jpa-converter
Advanced 10 steps
java
package com.acme.billing.config;
 
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;

Feature-flagged beans with Spring @ConditionalOnProperty

feature-flags conditional-beans strategy-pattern
Intermediate 5 steps
java
public static Map<String, String> parseCookieHeader(String header) {
    Map<String, String> cookies = new LinkedHashMap<>();
    if (header == null || header.isBlank()) {
        return cookies;

Parsing an HTTP Cookie header in Java

string-parsing http url-decoding
Intermediate 6 steps
java
public class TimedSocketReader {
 
    private static final int READ_TIMEOUT_MS = 5_000;
    private static final int CONNECT_TIMEOUT_MS = 3_000;

Reading a socket with connect and read timeouts

sockets timeouts io
Intermediate 8 steps
java
public final class EncodingDetector {
 
    public enum Encoding {
        UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN

Detecting text encoding from raw bytes in Java

byte-manipulation encoding-detection bitwise-operations
Intermediate 8 steps
java
public final class ImportOrganizer {
 
    private static final Pattern IMPORT_LINE =
            Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");

Sorting Java imports with a regex pass

regex sorting text-processing
Intermediate 8 steps

Share this explainer

Here's the card — post it anywhere.

Testing a JPA repository against real Postgres in Spring — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code