简体   繁体   English

Axon 框架:聚合自动装配 Bean 在测试中抛出 NullPointerException

[英]Axon Framework: Aggregate Autowired Bean throws NullPointerException in Test

Springboot and Axon: Basically I am unit testing an aggregate that uses three different ObjectMapper instances, each with a different configuration. Springboot 和 Axon:基本上,我正在对使用三个不同 ObjectMapper 实例的聚合进行单元测试,每个实例都具有不同的配置。 These are defined in config class:这些在配置 class 中定义:

    @Configuration
    public class JacksonConfiguration {

    @Bean(name="flatMapper")
    @Primary
    public ObjectMapper flatMapper(){
        return new ObjectMapper();
    }

    @Bean(name="unknownPropertiesMapper")
    public ObjectMapper unknownPropertiesMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }

    @Bean(name="nullPropertiesMapper")
    public ObjectMapper nullPropertiesMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper;
    }

}

They are injected and used in my aggregate as follow:它们被注入并在我的聚合中使用如下:

@Aggregate
@Data
@Component
public class MyAggregate {

    @Autowired
    @Qualifier("flatMapper")
    private ObjectMapper flatMapper;

    @Autowired
    @Qualifier("unknownPropertiesMapper")
    private ObjectMapper unknownPropertiesMapper;

    @Autowired
    @Qualifier("nullPropertiesMapper")
    private ObjectMapper nullPropertiesMapper;


    @AggregateIdentifier
    private String id;

  //Methods and Handlers: a method is using "unknownPropertiesMapper" is "changedKeySet" 

when I run SpringBootApplication everything is properly instanciated and working as expected, but when testing I get NullPointerException over thier instances:当我运行 SpringBootApplication 时,一切都已正确实例化并按预期工作,但在测试时,我在他们的实例上得到 NullPointerException:

@ContextConfiguration(classes = JacksonConfiguration.class)
@SpringBootTest(classes = OccurrenceAggregate.class)
@ComponentScan(basePackageClasses = MyAggregate.class)
public class AggregateTest {
    
    private FixtureConfiguration<MyAggregate> fixture;

    @BeforeEach
    public void setUp() {
        fixture = new AggregateTestFixture<>(MyAggregate.class);
    }

    @Test
    public void myTest() {

       fixture.givenNoPriorActivity().... 

    }

test console:测试控制台:

org.axonframework.test.AxonAssertionError: The command handler threw an unexpected exception

Expected <ANYTHING> but got <exception of type [NullPointerException]>. Stack trace follows:
java.lang.NullPointerException
    at com.example.business.aggregates.MyAggregate.changedKeySet(MyAggregate.java:185)

changedKeySet() is throwing NPE because its using unknownPropertiesMapper and it is value is null. as I mentionned it works fine when I run the Main class but not in tests (Junit5). changedKeySet()正在抛出 NPE,因为它使用unknownPropertiesMapper并且它的值为 null。正如我提到的,当我运行 Main class 但不是在测试(Junit5)中时它工作正常。

The Aggregate is not set up correctly.聚合设置不正确。 The correct way to inject a Spring Bean into the Aggregate is to add it to the CommandHandler method.将 Spring Bean 注入 Aggregate 的正确方法是将其添加到 CommandHandler 方法中。

@CommandHandler
    public void handle(ACommand cmd, ObjectMapper flatMapper) {

In the test fixture you can inject it this way:在测试夹具中,您可以这样注入它:

fixture.registerInjectableResource(flatMapper);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM