Test Class
@Autowired
private EventSourcingRepository customerEventSourcingRepository;
@Before
public void setUp() throws Exception {
fixture = Fixtures.newGivenWhenThenFixture(CustomerAggregate.class);
UpdateAddressCommandHandler commandHandler = new UpdateAddressCommandHandler();
commandHandler.setRepository(fixture.registerRepository(customerEventSourcingRepository));
fixture.registerAnnotatedCommandHandler(commandHandler);
}
}
@Test
public void testUpdateAddress() throws Exception {
Address oldaddress = new Address(“xyz”,"zyz,“zyz”);
Address newaddress =new Address(“xyz1”,"zyz1,“zyz1”);
fixture.given(new CustomerAddedEvent(“1”,“Mayuresh”, “Harsha”,“12345”, “xyz1.abc.com”, oldaddress, new Date()))
.when(new UpdateAddressCommand(“1”, newaddress))
.expectVoidReturnType()
.expectEvents(new AddressUpdatedEvent(“1”, newaddress));
}
UpdateAddressCommandHandler.java
@Component
public class UpdateAddressCommandHandler {
private static final Logger LOG = LoggerFactory.getLogger(UpdateAddressCommandHandler.class);
@Autowired
EventSourcingRepository customerEventSourcingRepository;
@CommandHandler
public void on(UpdateAddressCommand command) {
LOG.info(“Command: ‘UpdateAddressCommand’ received.”);
CustomerAggregate customerAggregate = (CustomerAggregate) customerEventSourcingRepository.load(command.getId());
customerAggregate.UpdateAddressCommand(command.getAddress());
}
public void setRepository(FixtureConfiguration registerRepository) {
this.customerEventSourcingRepository=(EventSourcingRepository) registerRepository;
}
}