我目前正在使用 Axon 4.2,并且我有一个在其方法中使用注入服务(CustomerService)的聚合(Customer)。@CommandHandlers
下面显示了它的简化版本(但对于本示例仍然有效)。
@Aggregate
public 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() 测试通过,因此问题似乎出在依赖注入中。
喵喔喔
郎朗坤
海绵宝宝撒
波斯汪
相关分类