如何测试 Spring 事件侦听器条件 SpEL?
How to test Spring event listener conditional SpEL?
我有一个带有条件语句的工作注释驱动事件侦听器。但是即使代码工作正常,由于处理 SpEL 条件的测试用例失败,我无法对该条件进行单元测试。
我注意到此错误仅发生在 Spring Boot 1.5.x 版本,因为 2.1.x 版本按预期工作。不幸的是我需要使用 1.5.x 版本。
Class 处理事件:
@Component
public class MyComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(MyComponent.class);
@EventListener(condition = "#createdEvent.awesome")
public void handleOrderCreatedEvent(OrderCreatedEvent createdEvent) {
LOGGER.info("Awesome event handled");
}
}
事件class:
public class OrderCreatedEvent {
public OrderCreatedEvent(boolean awesome) {
this.awesome = awesome;
}
private boolean awesome;
public boolean isAwesome() {
return awesome;
}
}
我的测试class:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyComponent.class)
public class DemoApplicationTests {
@Autowired
private ApplicationEventPublisher publisher;
@MockBean
private MyComponent myComponent;
@Test
public void handleOrderCreatedEvent_shouldExecute_whenAwesome() {
OrderCreatedEvent event = new OrderCreatedEvent(true);
publisher.publishEvent(event);
verify(myComponent).handleOrderCreatedEvent(event);
}
}
完整的源代码可以在这里找到:https://github.com/crazydevman/spring-event-testing
运行 应用程序一切正常。但是,当 运行 测试用例时,我不断收到此错误:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'awesome' cannot be found on null
调试代码,看起来这是由于 SpEL 无法解释模拟 bean 的方法参数名称 'createdEvent',但我不知道如何修复它。
有没有办法对事件条件进行单元测试?
@Component
public class MyComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(MyComponent.class);
@EventListener(condition = "#root.args[0].awesome")
public void handleOrderCreatedEvent(OrderCreatedEvent createdEvent) {
LOGGER.info("Awesome event handled");
}
}
我有一个带有条件语句的工作注释驱动事件侦听器。但是即使代码工作正常,由于处理 SpEL 条件的测试用例失败,我无法对该条件进行单元测试。
我注意到此错误仅发生在 Spring Boot 1.5.x 版本,因为 2.1.x 版本按预期工作。不幸的是我需要使用 1.5.x 版本。
Class 处理事件:
@Component
public class MyComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(MyComponent.class);
@EventListener(condition = "#createdEvent.awesome")
public void handleOrderCreatedEvent(OrderCreatedEvent createdEvent) {
LOGGER.info("Awesome event handled");
}
}
事件class:
public class OrderCreatedEvent {
public OrderCreatedEvent(boolean awesome) {
this.awesome = awesome;
}
private boolean awesome;
public boolean isAwesome() {
return awesome;
}
}
我的测试class:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyComponent.class)
public class DemoApplicationTests {
@Autowired
private ApplicationEventPublisher publisher;
@MockBean
private MyComponent myComponent;
@Test
public void handleOrderCreatedEvent_shouldExecute_whenAwesome() {
OrderCreatedEvent event = new OrderCreatedEvent(true);
publisher.publishEvent(event);
verify(myComponent).handleOrderCreatedEvent(event);
}
}
完整的源代码可以在这里找到:https://github.com/crazydevman/spring-event-testing
运行 应用程序一切正常。但是,当 运行 测试用例时,我不断收到此错误:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'awesome' cannot be found on null
调试代码,看起来这是由于 SpEL 无法解释模拟 bean 的方法参数名称 'createdEvent',但我不知道如何修复它。
有没有办法对事件条件进行单元测试?
@Component
public class MyComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(MyComponent.class);
@EventListener(condition = "#root.args[0].awesome")
public void handleOrderCreatedEvent(OrderCreatedEvent createdEvent) {
LOGGER.info("Awesome event handled");
}
}