亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Axon 夾具注入在 @CommandHandler 注解的方法中失?。嬙旌瘮党猓?/h1>

Axon 夾具注入在 @CommandHandler 注解的方法中失?。嬙旌瘮党猓?/h1>
牧羊人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(),只是為了檢查對注入資源的調用。

正如我所提到的,該代碼在我運行時有效?!癱ustomerService”是通過 a?SpringBeanParemeterResolver(我將其定義為 spring bean)注入的

單獨運行它們,結果是相同的: testCreation() 通過,而 testUpdate() 失敗并顯示錯誤消息No resource of type [CustomerService] has been registered對于這兩種情況,CustomerService 資源都通過 de @Before 方法注冊到 Fixture 中。

查看完整回答
反對 回復 2023-07-28
  • 4 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號