// Note: ProductJpaEntity is a JPA-annotated class that the domain never sees. // bootstrap/Application.java @SpringBootApplication public class Application public static void main(String[] args) SpringApplication.run(Application.class, args); // Manual wiring if not using Spring's @Autowired on fields @Bean public CreateProductService createProductService(ProductRepository repo) return new CreateProductService(repo);
class CreateProductServiceTest @Test void shouldSaveProduct() ProductRepository mockRepo = mock(ProductRepository.class); var service = new CreateProductService(mockRepo); var command = new CreateProductCommand("Mouse", 25, "USD"); Product result = service.execute(command); verify(mockRepo).save(any(Product.class)); assertEquals("Mouse", result.name()); designing hexagonal architecture with java pdf
@Override public Product execute(CreateProductCommand command) var id = UUID.randomUUID().toString(); var money = new Money(command.price(), command.currency()); var product = new Product(id, command.name(), money); productRepository.save(product); return product; // Note: ProductJpaEntity is a JPA-annotated class that
// domain/spi/ProductRepository.java (Outgoing Port) package com.example.domain.spi; import com.example.domain.model.Product; import java.util.Optional; var service = new CreateProductService(mockRepo)
public interface ProductRepository Optional<Product> findById(String id); void save(Product product);