简体   繁体   English

集成测试和spring应用程序事件

[英]Integration testing and spring application events

I have a spring rest controller which fires a ApplicationEvent 我有一个弹簧控制器,它可以激活一个ApplicationEvent

@RestController
public class VehicleController {

@Autowired
private VehicleService service;

@Autowired
private ApplicationEventPublisher eventPublisher;

@RequestMapping(value = "/public/rest/vehicle/add", method = RequestMethod.POST)
public void addVehicle(@RequestBody @Valid Vehicle vehicle){
    service.add(vehicle);
    eventPublisher.publishEvent(new VehicleAddedEvent(vehicle));
    }
}

And I have a integration test for the controller, something like 我对控制器进行了集成测试,例如

    @RunWith(SpringRunner.class)
    @WebMvcTest(controllers = VehicleController.class,includeFilters = @ComponentScan.Filter(classes = EnableWebSecurity.class))
    @Import(WebSecurityConfig.class)

public class VehicleControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
private VehicleService vehicleService;

@Test
public void addVehicle() throws Exception {
    Vehicle vehicle=new Vehicle();
    vehicle.setMake("ABC");
    ObjectMapper mapper=new ObjectMapper();
    String s = mapper.writeValueAsString(vehicle);

    given(vehicleService.add(vehicle)).willReturn(1);

    mockMvc.perform(post("/public/rest/vehicle/add").contentType(
            MediaType.APPLICATION_JSON).content(s))
            .andExpect(status().isOk());
   }
}

Now, if I remove the event publishing line, the test successes. 现在,如果我删除事件发布行,测试成功。 However, with the event, it hits error. 但是,对于该事件,它会发生错误。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: null source

I tried bunch of different things, to avoid or skip the line in testing but nothing helped. 我尝试了很多不同的东西,以避免或跳过测试中的线,但没有任何帮助。 Could you please tell me what is the right way to test such code? 你能否告诉我测试这些代码的正确方法是什么? Thanks in advance 提前致谢

I have reproduced this issue locally and this exception ... 我已在本地复制此问题,此异常......

org.springframework.web.util.NestedServletException: Request processing failed; org.springframework.web.util.NestedServletException:请求处理失败; nested exception is java.lang.IllegalArgumentException: null source 嵌套异常是java.lang.IllegalArgumentException:null source

... strongly implies that the constructor of your VehicleAddedEvent looks like this: ... 强烈暗示您的VehicleAddedEvent的构造函数如下所示:

public VehicleAddedEvent(Vehicle vehicle) {
    super(null);
}

If you look further down the stacktrace you'll likely see something like this: 如果你向下看堆栈跟踪,你可能会看到这样的东西:

Caused by: java.lang.IllegalArgumentException: null source
    at java.util.EventObject.<init>(EventObject.java:56)
    at org.springframework.context.ApplicationEvent.<init>(ApplicationEvent.java:42)

So, in answer to your question; 那么,回答你的问题; the issue is not with your test, it is with the super call in the VehicleAddedEvent constructor and if you update that such that is calls super(vehicle) rather than super(null) then the event publication will not throw an exception. 问题不在于你的测试,它是在VehicleAddedEvent构造函数中的超级调用,如果你更新那个调用super(vehicle)而不是super(null)那么事件发布不会抛出异常。

This will allow your test to complete although there is nothing in your test which asserts on or verifies that this event has been published so you may want to look into adding something for that. 这将允许您的测试完成,尽管您的测试中没有任何内容断言或验证此事件已发布,因此您可能需要考虑添加一些内容。 You probably already have an implementation of ApplicationListener<Vehicle> (if not then I'm not sure what the benefit of publishing the 'vehicle event' is) so you could @Autowire that into VehicleControllerTest and verify that the vehicle event was published like this perhaps: 你可能已经有了ApplicationListener<Vehicle>的实现(如果没有那么我不确定发布'车辆事件'的好处是什么)所以你可以@Autowire进入VehicleControllerTest并验证车辆事件是否像这样发布也许:

// provide some public accessor which allows a caller to ask your custom
// application listener whether it has received a specific event
Assert.assertTrue(applicationListener.received(vehicle));

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

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