java - 轴突测试 : Missing Context

标签 java testing spring-boot axon

在 Axon-SpringBoot 应用程序中,我有一个聚合,它在某些命令处理程序中使用注入(inject)的 DAO。

例如:

@Aggregate
class MyAggregate {

    @CommandHandler
    public MyAggregate (CreateMyAggregateCommand command, @Autowired MyAggregateDao dao) {

          final SomeProperty = command.getSomePoprtery();

          if (dao.findBySomeProperty(someProperty) == null) {
               AggregateLifeCycle.apply(
                     new MyAggregateCreatedEvent(command.getIdentifier(),
                                                 someProperty);
          } else {
               // Don't create, already exits with some property
               // report ...
          }
    }

}

像这样的标准测试

@Test
void creationSucceeds () {

    aggregateTestFixture = new AggregateTestFixture<>(MyAggregate.class);

    final CreateMyAggregateCommand command = new CreateMyAggregateCommand(...);
    final MyAggregateCreatedEvent = new MyAggregateCreatedEvent(...);

    aggregateTestFixture
            .givenNoPriorActivity()
            .when(command)
            .expectEvents(event);

}

失败:

org.axonframework.test.FixtureExecutionException: No resource of type 
[com.xmpl.MyAggregateDao] has been registered. It is required 
for one of the handlers being executed.

我如何提供测试实现?

最佳答案

解决方案1:模拟

由于这是关于单元测试并且我的问题涉及数据库调用(外部服务),因此只要测试仅涉及孤立的聚合行为,模拟似乎就适用。

@Test
void creationSucceeds () {

    aggregateTestFixture = new AggregateTestFixture<>(MyAggregate.class);
    aggregateTestFixture.registerInjectableResource(
          Mockito.mock(MyAggregateDao.class));

}

解决方案 2:真实注入(inject)(jUnit 5)

这个对我有用:

  1. this github repo 获取用于 Spring jUnit5 测试支持的小型库
  2. 注释测试类:

    @SpringBootTest(classes = {SpringTestConfig.class}) 
    @ExtendWith(SpringExtension.class)
    public class MyAggregateTest {
    
        // ...        
    
    }
    
  3. 将 application.properties 放入 src/test/resources

  4. 编写启动功能齐全的 Spring 容器的 Spring 测试配置:

    @EnableAutoConfiguration
    public class SpringTestConfig {
    
         // Set up whatever you need
    
         @Bean
         @Autowired
         MyAggregateDao myDao (DataSource dataSource) {
    
             // ...
         }
    
    
         @Bean
         @Autowired
         EventStorageEngine eventStorageEngine () {
    
             return new InMemoryEventStorageEngine();
    
         }
    
    }
    
  5. 直接注入(inject)到您的测试中,配置AggregateTestFixture

    private AggregateTestFixture<MyAggregate> aggregateTestFixture;
    
    @Autowired
    private MyAggregateDao myDao;
    
    @BeforeEach
    void beforeEach () {
    
        aggregateTestFixture = new AggregateTestFixture<>(MyAggregate.class);
    
        // We still need to register resources manually
        aggregateTestFixture.registerInjectableResource(myDao);
    
    }
    

使用 jUnit 4

设置一个使用 jUnit 4 启动 Spring 容器的测试配置有点不同,但有足够的文档。开始here

关于java - 轴突测试 : Missing Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45160938/

相关文章:

java - 使用 Java 加载 .NET 4.5 可执行文件

java - 如何解决 Java 代码中的 ORA-01795

java - 弹出 ListView 出现在另一个 ListView 的 ItemClickListener 上

api - 数据可视化API

ios - 从设备清除 IAP 测试用户历史记录

testing - 测试错误 : how to use :id?

64 位 Windows 10 上的 Java 32 位内存阈值

html - 如何使用Gradle将HTML页面放入Spring-boot应用程序

excel - 无法使用 Spring Boot 和 Apache POI 正确生成 Excel 文件

java - 服务 API 网关