@Test
 public void testFixture_UnexpectedReturnValue() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   try {
     fixture
         .registerAnnotatedCommandHandler(commandHandler)
         .given(givenEvents)
         .when(new TestCommand("aggregateId"))
         .expectException(RuntimeException.class);
     fail("Expected an AxonAssertionError");
   } catch (AxonAssertionError e) {
     assertTrue(
         e.getMessage()
             .contains("The command handler returned normally, but an exception was expected"));
     assertTrue(
         e.getMessage()
             .contains("<an instance of java.lang.RuntimeException> but returned with <null>"));
   }
 }
 @Test
 public void testAggregateIdentifier_ServerGeneratedIdentifier() {
   fixture.registerAggregateFactory(mockAggregateFactory);
   fixture.registerAnnotatedCommandHandler(
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));
   fixture.givenNoPriorActivity().when(new CreateAggregateCommand());
 }
 @Test
 public void testFixture_WrongEventContents_WithNullValues() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   try {
     fixture
         .registerAnnotatedCommandHandler(commandHandler)
         .given(givenEvents)
         .when(new TestCommand("aggregateId"))
         .expectEvents(new MyEvent("aggregateId", null)) // should be 4
         .expectVoidReturnType();
     fail("Expected an AxonAssertionError");
   } catch (AxonAssertionError e) {
     assertTrue(
         e.getMessage()
             .contains(
                 "In an event of type [MyEvent], the property [someValue] was not as expected."));
     assertTrue(e.getMessage().contains("Expected <<null>> but got <4>"));
   }
 }
 @Test(expected = FixtureExecutionException.class)
 public void testInjectResources_CommandHandlerAlreadyRegistered() {
   fixture.registerAggregateFactory(mockAggregateFactory);
   fixture.registerAnnotatedCommandHandler(
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));
   fixture.registerInjectableResource("I am injectable");
 }
 @Test
 public void testFixtureDetectsStateChangeOutsideOfHandler_AggregateGeneratesIdentifier() {
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .given()
       .when(new CreateAggregateCommand(null));
 }
 @Test
 public void testFixture_AggregateDeleted() {
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .given(new MyEvent("aggregateId", 5))
       .when(new DeleteCommand("aggregateId", false))
       .expectEvents(new MyAggregateDeletedEvent(false));
 }
 @Test
 public void testFixture_NoEventsInStore() {
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .given()
       .when(new TestCommand(UUID.randomUUID()))
       .expectException(AggregateNotFoundException.class);
 }
 @Test
 public void testStoringExistingAggregateGeneratesException() {
   fixture.registerAggregateFactory(mockAggregateFactory);
   fixture.registerAnnotatedCommandHandler(
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));
   fixture
       .given(new MyEvent("aggregateId", 1))
       .when(new CreateAggregateCommand("aggregateId"))
       .expectException(EventStoreException.class);
 }
 @Test
 public void testFirstFixture() {
   ResultValidator validator =
       fixture
           .registerAnnotatedCommandHandler(
               new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
           .given(new MyEvent("aggregateId", 1))
           .when(new TestCommand("aggregateId"));
   validator.expectReturnValue(null);
   validator.expectEvents(new MyEvent("aggregateId", 2));
 }
 @Test
 public void testFixture_SetterInjection() {
   MyCommandHandler commandHandler = new MyCommandHandler();
   commandHandler.setRepository(fixture.getRepository());
   fixture
       .registerAnnotatedCommandHandler(commandHandler)
       .given(new MyEvent("aggregateId", 1), new MyEvent("aggregateId", 2))
       .when(new TestCommand("aggregateId"))
       .expectReturnValue(IsNull.nullValue())
       .expectEvents(new MyEvent("aggregateId", 3));
 }
  @SuppressWarnings("unchecked")
  @Test
  public void testConfigureCustomAggregateFactory() {
    fixture.registerAggregateFactory(mockAggregateFactory);
    fixture.registerAnnotatedCommandHandler(
        new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));

    fixture.given(new MyEvent("id1", 1)).when(new TestCommand("id1"));

    verify(mockAggregateFactory).createAggregateRoot(eq("id1"), isA(DomainEventMessage.class));
  }
 @Test
 public void testExpectEventsIgnoresFilteredField() {
   ResultValidator validator =
       fixture
           .registerAnnotatedCommandHandler(
               new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
           .registerFieldFilter(field -> !field.getName().equals("someBytes"))
           .given(new MyEvent("aggregateId", 1))
           .when(new TestCommand("aggregateId"));
   validator.expectReturnValue(null);
   validator.expectEvents(new MyEvent("aggregateId", 2, "ignored".getBytes()));
 }
 @Test
 public void testFixtureGivenCommands() {
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .givenCommands(
           new CreateAggregateCommand("aggregateId"),
           new TestCommand("aggregateId"),
           new TestCommand("aggregateId"),
           new TestCommand("aggregateId"))
       .when(new TestCommand("aggregateId"))
       .expectEvents(new MyEvent("aggregateId", 4));
 }
 @Test
 public void testFixtureDetectsStateChangeOutsideOfHandler_Ignored() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   fixture.setReportIllegalStateChange(false);
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .given(givenEvents)
       .when(new IllegalStateChangeCommand("aggregateId", null));
 }
 @Test
 public void testFixtureIgnoredStateChangeInFilteredField() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   fixture.registerFieldFilter(field -> !field.getName().equals("lastNumber"));
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .given(givenEvents)
       .when(new IllegalStateChangeCommand("aggregateId", 5));
 }
 @Test
 public void testReadAggregate_WrongIdentifier() {
   fixture.registerAggregateFactory(mockAggregateFactory);
   fixture.registerAnnotatedCommandHandler(
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));
   TestExecutor exec = fixture.given(new MyEvent("AggregateId", 1));
   try {
     exec.when(new TestCommand("OtherIdentifier"));
     fail("Expected an AssertionError");
   } catch (AssertionError e) {
     assertTrue(
         "Wrong message. Was: " + e.getMessage(), e.getMessage().contains("OtherIdentifier"));
     assertTrue("Wrong message. Was: " + e.getMessage(), e.getMessage().contains("AggregateId"));
   }
 }
 @Test
 public void testFixtureDetectsStateChangeOutsideOfHandler_AggregateDeleted() {
   TestExecutor exec =
       fixture
           .registerAnnotatedCommandHandler(
               new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
           .given(new MyEvent("aggregateId", 5));
   try {
     exec.when(new DeleteCommand("aggregateId", true));
     fail("Fixture should have failed");
   } catch (AssertionError error) {
     assertTrue(
         "Wrong message: " + error.getMessage(),
         error.getMessage().contains("considered deleted"));
   }
 }
 @Test
 public void testFixture_GivenAList() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   fixture
       .registerAnnotatedCommandHandler(
           new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
       .given(givenEvents)
       .when(new TestCommand("aggregateId"))
       .expectEvents(new MyEvent("aggregateId", 4))
       .expectStoredEvents(new MyEvent("aggregateId", 4))
       .expectPublishedEvents(new MyEvent("aggregateId", 4))
       .expectVoidReturnType();
 }
 @Test(expected = EventStoreException.class)
 public void testFixtureGeneratesExceptionOnWrongEvents_WrongSequence() {
   String identifier = UUID.randomUUID().toString();
   fixture
       .getEventStore()
       .publish(
           new GenericDomainEventMessage<>("test", identifier, 0, new StubDomainEvent()),
           new GenericDomainEventMessage<>("test", identifier, 2, new StubDomainEvent()));
 }
 @Before
 public void setUp() {
   fixture = new AggregateTestFixture<>(StandardAggregate.class);
   fixture.setReportIllegalStateChange(false);
   mockAggregateFactory = mock(AggregateFactory.class);
   when(mockAggregateFactory.getAggregateType()).thenReturn(StandardAggregate.class);
   when(mockAggregateFactory.createAggregateRoot(isA(String.class), isA(DomainEventMessage.class)))
       .thenReturn(new StandardAggregate("id1"));
 }
 @Test(expected = EventStoreException.class)
 public void testFixtureGeneratesExceptionOnWrongEvents_DifferentAggregateIdentifiers() {
   fixture
       .getEventStore()
       .publish(
           new GenericDomainEventMessage<>(
               "test", UUID.randomUUID().toString(), 0, new StubDomainEvent()),
           new GenericDomainEventMessage<>(
               "test", UUID.randomUUID().toString(), 0, new StubDomainEvent()));
 }
 @Test
 public void testFixture_ReportWrongNumberOfEvents() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   try {
     fixture
         .registerAnnotatedCommandHandler(commandHandler)
         .given(givenEvents)
         .when(new TestCommand("aggregateId"))
         .expectEvents(new MyEvent("aggregateId", 4), new MyEvent("aggregateId", 5));
     fail("Expected an AxonAssertionError");
   } catch (AxonAssertionError e) {
     assertTrue(e.getMessage().contains("org.axonframework.test.MyEvent <|> "));
   }
 }
 @Test
 public void testFixtureDetectsStateChangeOutsideOfHandler_NullValue() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   try {
     fixture
         .registerAnnotatedCommandHandler(
             new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()))
         .given(givenEvents)
         .when(new IllegalStateChangeCommand("aggregateId", null));
     fail("Expected AssertionError");
   } catch (AssertionError e) {
     assertTrue("Wrong message: " + e.getMessage(), e.getMessage().contains(".lastNumber\""));
     assertTrue("Wrong message: " + e.getMessage(), e.getMessage().contains("<null>"));
     assertTrue("Wrong message: " + e.getMessage(), e.getMessage().contains("<4>"));
   }
 }
 @Test
 public void testFixture_CommandHandlerDispatchesNonDomainEvents() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   // the domain events are part of the transaction, but the command handler directly dispatches an
   // application
   // event to the event bus. This event dispatched anyway. The
   fixture
       .registerAnnotatedCommandHandler(commandHandler)
       .given(givenEvents)
       .when(new StrangeCommand("aggregateId"))
       .expectStoredEvents()
       .expectPublishedEvents(new MyApplicationEvent())
       .expectException(StrangeCommandReceivedException.class);
 }
 @Test
 public void testFixture_WrongReturnValue() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   try {
     fixture
         .registerAnnotatedCommandHandler(commandHandler)
         .given(givenEvents)
         .when(new TestCommand("aggregateId"))
         .expectReturnValue("some");
     fail("Expected an AxonAssertionError");
   } catch (AxonAssertionError e) {
     assertTrue(e.getMessage(), e.getMessage().contains("<\"some\"> but got <null>"));
   }
 }
 @Test
 public void testFixture_UnexpectedException() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   try {
     fixture
         .registerAnnotatedCommandHandler(commandHandler)
         .given(givenEvents)
         .when(new StrangeCommand("aggregateId"))
         .expectVoidReturnType();
     fail("Expected an AxonAssertionError");
   } catch (AxonAssertionError e) {
     assertTrue(
         e.getMessage().contains("but got <exception of type [StrangeCommandReceivedException]>"));
   }
 }
 @Test
 public void testFixture_ExpectedPublishedSameAsStored() {
   List<?> givenEvents =
       Arrays.asList(
           new MyEvent("aggregateId", 1),
           new MyEvent("aggregateId", 2),
           new MyEvent("aggregateId", 3));
   MyCommandHandler commandHandler =
       new MyCommandHandler(fixture.getRepository(), fixture.getEventBus());
   try {
     fixture
         .registerAnnotatedCommandHandler(commandHandler)
         .given(givenEvents)
         .when(new StrangeCommand("aggregateId"))
         .expectEvents(new MyEvent("aggregateId", 4)) // should be 4
         .expectException(StrangeCommandReceivedException.class);
     fail("Expected an AxonAssertionError");
   } catch (AxonAssertionError e) {
     assertTrue(e.getMessage().contains("The stored events do not match the published events."));
     assertTrue(e.getMessage().contains(" <|> org.axonframework.test.MyApplicationEvent"));
     assertTrue(e.getMessage().contains("probable cause"));
   }
 }
  @Test
  public void testAggregateIdentifier_IdentifierAutomaticallyDeducted() {
    fixture.registerAggregateFactory(mockAggregateFactory);
    fixture.registerAnnotatedCommandHandler(
        new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));
    fixture
        .given(new MyEvent("AggregateId", 1), new MyEvent("AggregateId", 2))
        .when(new TestCommand("AggregateId"))
        .expectEvents(new MyEvent("AggregateId", 3));

    DomainEventStream events = fixture.getEventStore().readEvents("AggregateId");
    for (int t = 0; t < 3; t++) {
      assertTrue(events.hasNext());
      DomainEventMessage next = events.next();
      assertEquals("AggregateId", next.getAggregateIdentifier());
      assertEquals(t, next.getSequenceNumber());
    }
  }
 @Before
 public void setUp() {
   fixture = Fixtures.newGivenWhenThenFixture(StandardAggregate.class);
   fixture.registerAggregateFactory(new StandardAggregate.Factory());
 }
 @SuppressWarnings("unchecked")
 @Test(expected = IncompatibleAggregateException.class)
 public void
     testConfigurationOfRequiredCustomAggregateFactoryNotProvided_FailureOnGetRepository() {
   fixture.getRepository();
 }