Skip to content

Commit bd4881d

Browse files
committed
BAEL-2435 Update Aggregate in regards to latest axon version
-Replace old import statements for the new locations -Mark the aggregate as @aggregate to make it autowired through the spring boot starter -Move all command handlers/decision making functions to the top of the file -Rename the command handler to `handle` as a best practice for command handlers -Use @EventSourcingHandler i.o. @eventhandler as a best practice for event handlers in Aggregates, as those only are used to 'source' the aggregate -Update test case according to latest standards
1 parent cf56b33 commit bd4881d

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.baeldung.axon.aggregates;
22

3+
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
4+
5+
import org.axonframework.commandhandling.CommandHandler;
6+
import org.axonframework.eventsourcing.EventSourcingHandler;
7+
import org.axonframework.modelling.command.AggregateIdentifier;
8+
import org.axonframework.spring.stereotype.Aggregate;
9+
310
import com.baeldung.axon.commands.CreateMessageCommand;
411
import com.baeldung.axon.commands.MarkReadMessageCommand;
512
import com.baeldung.axon.events.MessageCreatedEvent;
613
import com.baeldung.axon.events.MessageReadEvent;
7-
import org.axonframework.commandhandling.CommandHandler;
8-
import org.axonframework.commandhandling.model.AggregateIdentifier;
9-
import org.axonframework.eventhandling.EventHandler;
10-
11-
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;
12-
1314

15+
@Aggregate
1416
public class MessagesAggregate {
1517

1618
@AggregateIdentifier
@@ -24,13 +26,13 @@ public MessagesAggregate(CreateMessageCommand command) {
2426
apply(new MessageCreatedEvent(command.getId(), command.getText()));
2527
}
2628

27-
@EventHandler
28-
public void on(MessageCreatedEvent event) {
29-
this.id = event.getId();
30-
}
31-
3229
@CommandHandler
33-
public void markRead(MarkReadMessageCommand command) {
30+
public void handle(MarkReadMessageCommand command) {
3431
apply(new MessageReadEvent(id));
3532
}
33+
34+
@EventSourcingHandler
35+
public void on(MessageCreatedEvent event) {
36+
this.id = event.getId();
37+
}
3638
}

axon/src/test/java/com/baeldung/axon/MessagesAggregateIntegrationTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
package com.baeldung.axon;
22

3+
import java.util.UUID;
4+
5+
import org.axonframework.test.aggregate.AggregateTestFixture;
6+
import org.axonframework.test.aggregate.FixtureConfiguration;
7+
import org.junit.*;
8+
39
import com.baeldung.axon.aggregates.MessagesAggregate;
410
import com.baeldung.axon.commands.CreateMessageCommand;
511
import com.baeldung.axon.commands.MarkReadMessageCommand;
612
import com.baeldung.axon.events.MessageCreatedEvent;
713
import com.baeldung.axon.events.MessageReadEvent;
8-
import org.axonframework.test.aggregate.AggregateTestFixture;
9-
import org.axonframework.test.aggregate.FixtureConfiguration;
10-
import org.junit.Before;
11-
import org.junit.Test;
12-
13-
import java.util.UUID;
1414

1515
public class MessagesAggregateIntegrationTest {
1616

1717
private FixtureConfiguration<MessagesAggregate> fixture;
1818

1919
@Before
20-
public void setUp() throws Exception {
21-
fixture = new AggregateTestFixture<MessagesAggregate>(MessagesAggregate.class);
22-
20+
public void setUp() {
21+
fixture = new AggregateTestFixture<>(MessagesAggregate.class);
2322
}
2423

2524
@Test
26-
public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() throws Exception {
25+
public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() {
2726
String eventText = "Hello, how is your day?";
2827
String id = UUID.randomUUID().toString();
2928
fixture.given()
@@ -32,7 +31,7 @@ public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageC
3231
}
3332

3433
@Test
35-
public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() throws Exception {
34+
public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() {
3635
String id = UUID.randomUUID().toString();
3736

3837
fixture.given(new MessageCreatedEvent(id, "Hello :-)"))

0 commit comments

Comments
 (0)