为了账号安全,请及时绑定邮箱和手机立即绑定

Axon 夹具注入在 @CommandHandler 注解的方法中失败(构造函数除外)

Axon 夹具注入在 @CommandHandler 注解的方法中失败(构造函数除外)

牧羊人nacy 2023-07-28 15:47:26
我目前正在使用 Axon 4.2,并且我有一个在其方法中使用注入服务(CustomerService)的聚合(Customer)。@CommandHandlers下面显示了它的简化版本(但对于本示例仍然有效)。@Aggregatepublic class Customer {  @AggregateIdentifier  private Long id;  private String name;  private String address;  @CommandHandler  public Customer(CreateCommand command, CustomerService customerService) {    log.debug( customerService.doSomething(command.getId()));    AggregateLifecycle.apply(new CreatedEvent(command.getId(), command.getName()));  }  @CommandHandler  public void on(UpdateCommand command, CustomerService customerService){    log.debug( customerService.doSomething(command.getId()));    AggregateLifecycle.apply( new UpdatedEvent(command.getId(),command.getAddress()));  }  @EventSourcingHandler  public void on(CreatedEvent event){    this.id = event.getId();    this.name = event.getName();  }  @EventSourcingHandler  public void on(UpdatedEvent event){      this.address = event.getAddress();  }}这是相应的测试:@RunWith(MockitoJUnitRunner.class)public class CustomerTest {  @Mock  private CustomerService customerService;  private FixtureConfiguration<Customer> fixture;  @Before  public void setUp() {    fixture = new AggregateTestFixture<>(Customer.class);    fixture.registerInjectableResource(customerService);  }  @Test  public void testCreation(){    final Long id = 1L;    final String name = "Elmo";    when(customerService.doSomething(id)).thenReturn("called");    fixture.givenNoPriorActivity()            .when(new CreateCommand(id, name))            .expectEvents(new CreatedEvent(id, name));    verify(customerService).doSomething(id);    verifyNoMoreInteractions(customerService);  }  @Test  public void testUpdate(){    final Long id = 1L;    final String name = "Elmo";    final String address = "Sesame street";    when(customerService.doSomething(id)).thenReturn("called");  }}如果我删除 on UpdateCommand 方法中的 CustomerService 参数(以及相关代码),则 testUpdate() 测试通过,因此问题似乎出在依赖注入中。
查看完整描述

4 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

在 Axon 中,聚合接受业务命令,这通常会产生与业务领域相关的事件——领域事件。CustomerService首先,您不应该将逻辑委托给某些外部服务。



查看完整回答
反对 回复 2023-07-28
?
郎朗坤

TA贡献1921条经验 获得超9个赞

首先,我想指出伊万·杜加利奇(Ivan Dugalic)的观点非常好。聚合应该处理业务逻辑,而不是将其提供给服务。您可能会考虑注入域服务,在这方面它本质上应该被视为状态机。

除了设计问题之外,眼前的问题仍然很奇怪。您已经正确定义了一个CustomerService模拟,并且重要的是Fixture使用该registerInjectableResource方法将其注册到了。

您是否尝试过分别运行testCreationtestUpdate测试?如果是这样,您还会遇到同样的异常吗?如果后面的问题也得到肯定的回答,我个人需要进行一些调试才能找出原因CustomerService

  1. 根本没有注册

  2. 设置为null,因此不可“注射”

  3. 在测试周期内的任意一处移除

希望以上内容能够指导您找到正确的解决方案 Ernesto!


查看完整回答
反对 回复 2023-07-28
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

设置了固定装置fixture.givenState(() -> new Customer(id, name, null)),它应该是fixture.given(new CreatedEvent(id, name))


查看完整回答
反对 回复 2023-07-28
?
波斯汪

TA贡献1811条经验 获得超4个赞

在示例中将其称为“CustomerService”,就像我可以将其称为“DomainService”、“ComplexMathDomainCalculationUtils”或“DomainLogicExtractedToAnotherClassBecauseItWasTooBigAndComplexToBeThere”;-)我只是想展示一个依赖注入的示例,为此我使用了一个日志.debug(),只是为了检查对注入资源的调用。

正如我所提到的,该代码在我运行时有效。“customerService”是通过 a SpringBeanParemeterResolver(我将其定义为 spring bean)注入的

单独运行它们,结果是相同的: testCreation() 通过,而 testUpdate() 失败并显示错误消息No resource of type [CustomerService] has been registered对于这两种情况,CustomerService 资源都通过 de @Before 方法注册到 Fixture 中。

查看完整回答
反对 回复 2023-07-28
  • 4 回答
  • 0 关注
  • 106 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信