diff --git a/jmolecules-spring-data-jpa/src/main/java/org/jmolecules/examples/jpa/customer/Customer.java b/jmolecules-spring-data-jpa/src/main/java/org/jmolecules/examples/jpa/customer/Customer.java index 5672f54..fdcf70d 100644 --- a/jmolecules-spring-data-jpa/src/main/java/org/jmolecules/examples/jpa/customer/Customer.java +++ b/jmolecules-spring-data-jpa/src/main/java/org/jmolecules/examples/jpa/customer/Customer.java @@ -24,6 +24,7 @@ import org.jmolecules.ddd.types.AggregateRoot; import org.jmolecules.ddd.types.Identifier; +import org.jmolecules.ddd.types.ValueObject; import org.jmolecules.examples.jpa.customer.Customer.CustomerId; import org.springframework.util.Assert; @@ -34,7 +35,7 @@ public class Customer implements AggregateRoot { private final CustomerId id; - private String firstname, lastname; + private Name name; private List
addresses; public Customer(String firstname, String lastname, Address address) { @@ -43,13 +44,13 @@ public Customer(String firstname, String lastname, Address address) { this.id = CustomerId.of(UUID.randomUUID().toString()); - this.firstname = firstname; - this.lastname = lastname; - + this.name = new Name(firstname, lastname); this.addresses = new ArrayList<>(); this.addresses.add(address); } + public record Name(String firstname, String lastname) implements ValueObject {} + @Value(staticConstructor = "of") public static class CustomerId implements Identifier { private final String id; diff --git a/jmolecules-spring-data-jpa/src/test/java/org/jmolecules/examples/jpa/ApplicationIntegrationTests.java b/jmolecules-spring-data-jpa/src/test/java/org/jmolecules/examples/jpa/ApplicationIntegrationTests.java index ebd4cc3..fc2cb49 100644 --- a/jmolecules-spring-data-jpa/src/test/java/org/jmolecules/examples/jpa/ApplicationIntegrationTests.java +++ b/jmolecules-spring-data-jpa/src/test/java/org/jmolecules/examples/jpa/ApplicationIntegrationTests.java @@ -17,6 +17,7 @@ import static org.assertj.core.api.Assertions.*; +import jakarta.persistence.EntityManager; import lombok.RequiredArgsConstructor; import org.jmolecules.examples.jpa.customer.Address; @@ -29,15 +30,20 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.TestConstructor; +import org.springframework.test.context.TestConstructor.AutowireMode; +import org.springframework.transaction.annotation.Transactional; /** * @author Oliver Drotbohm */ @SpringBootTest +@TestConstructor(autowireMode = AutowireMode.ALL) @RequiredArgsConstructor class ApplicationIntegrationTests { private final ConfigurableApplicationContext context; + private final EntityManager em; @Test void bootstrapsContainer() { @@ -55,6 +61,7 @@ void bootstrapsContainer() { } @Test // #24 + @Transactional void exposesPersistenceComponents() { var address = new Address("41 Greystreet", "Dreaming Tree", "2731"); @@ -65,6 +72,12 @@ void exposesPersistenceComponents() { var orders = context.getBean(Orders.class); var order = orders.save(new Order(customer)); - customers.resolveRequired(order.getCustomer()); + em.flush(); + em.clear(); + + var resolved = customers.resolveRequired(order.getCustomer()); + + assertThat(resolved.getName().firstname()).isEqualTo("Dave"); + assertThat(resolved.getName().lastname()).isEqualTo("Matthews"); } }