Axon 框架:聚合 Autowired Bean 在测试中抛出 NullPointerException
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
聚合设置不正确。将 Spring Bean 注入聚合的正确方法是将其添加到 CommandHandler 方法中。
在测试装置中,您可以这样注入:
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.
In the test fixture you can inject it this way: