Axon 框架:聚合 Autowired Bean 在测试中抛出 NullPointerException

发布于 2025-01-10 02:35:26 字数 2420 浏览 1 评论 0原文

Springboot 和 Axon:基本上,我正在对使用三个不同 ObjectMapper 实例的聚合进行单元测试,每个实例具有不同的配置。这些是在配置类中定义的:

    @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;
    }

}

它们被注入并在我的聚合中使用,如下所示:

@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" 

当我运行 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().... 

    }

测试控制台:

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() 抛出 NPE,因为它使用 unknownPropertiesMapper 并且它的值为 null。 正如我所提到的,当我运行 Main 类但在测试中(Junit5)时它工作得很好。

Springboot and Axon: Basically I am unit testing an aggregate that uses three different ObjectMapper instances, each with a different configuration. These are defined in config 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:

@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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2025-01-17 02:35:27

聚合设置不正确。将 Spring Bean 注入聚合的正确方法是将其添加到 CommandHandler 方法中。

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

在测试装置中,您可以这样注入:

fixture.registerInjectableResource(flatMapper);

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.

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

In the test fixture you can inject it this way:

fixture.registerInjectableResource(flatMapper);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文