Annotation Interface CommandHandlingTest


@Target(TYPE) @Retention(RUNTIME) @Documented @Inherited @ExtendWith(org.springframework.test.context.junit.jupiter.SpringExtension.class) @BootstrapWith(org.springframework.boot.test.context.SpringBootTestContextBootstrapper.class) @OverrideAutoConfiguration(enabled=false) @TypeExcludeFilters(com.opencqrs.framework.command.CommandHandlingTestExcludeFilter.class) @ImportAutoConfiguration public @interface CommandHandlingTest
Annotation that can be used for Spring Boot tests that focus only on CQRS CommandHandlerDefinitions in favor of initializing CommandHandlingTestFixture manually. This annotation provides a lazy CommandHandlingTestFixture per CommandHandlerDefinition bean or CommandHandling annotated method, which may be auto-wired into test methods directly. The fixture is configured with all StateRebuildingHandlerDefinitions found within the context and the CommandHandlerDefinition under test. A typical test annotated with CommandHandlingTest may look like this:
 @CommandHandlingTest
 public class BookAggregateTest {

     @Test
     public void bookAdded(@Autowired CommandHandlingTestFixture<BookAggregate, AddBookCommand, UUID> fixture) {
          UUID bookId = UUID.randomUUID();
          fixture
              .givenNothing()
              .when(
                  new AddBookCommand(
                      bookId,
                      "Tolkien",
                      "LOTR",
                      "DE234723432"
                  )
              )
              .expectSuccessfulExecution()
              .expectSingleEvent(
                  new BookAddedEvent(
                      bookId,
                      "Tolkien",
                      "LOTR",
                      "DE234723432"
                  )
              );
     }
 }
 

Using this annotation will disable full auto-configuration and instead apply only configurations relevant to initialize CommandHandlingTestFixture, i.e. CommandHandlerDefinitions and StateRebuildingHandlerDefinitions, but not Component or Beans.

In order for this annotation to be able to initialize CommandHandlerDefinition, CommandHandling methods, and StateRebuildingHandlerDefinition beans, these should be defined within a CommandHandlerConfiguration. Any dependent beans required for initializing them are typically provided by defining them as @MockitoBean within the test annotated using this.

See Also: