Testing integration and spring application events

I have a spring rest controller that runs 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 an 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 delete the event publishing line, the test results. However, with an event, he falls into error.

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

I tried a bunch of different things to avoid or skip the testing line, but nothing helped. Could you tell me how to properly test such code? thanks in advance

+5
source share
1 answer

I reproduced this problem locally and this exception ...

org.springframework.web.util.NestedServletException: request processing failed; Nested exception - java.lang.IllegalArgumentException: null source

... strong implies that the constructor of your VehicleAddedEvent looks like this:

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

If you look further on the stack, you are likely to 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 response to your question; the problem is not with your test, but with a super call in the VehicleAddedEvent constructor, and if you update this, that is, calls to super(vehicle) rather than super(null) , posting events will not raise an exception.

This will allow you to complete your test, although there is nothing in your test that claims or verifies that this event was posted, so you might want to learn something for this. You probably already have an implementation of ApplicationListener<Vehicle> (if not, I'm not sure what the benefit of posting a โ€œvehicle eventโ€ is, so that you can @Autowire insert VehicleControllerTest and make sure that the vehicle event has been posted as possible :

 // 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)); 
+6
source

Source: https://habr.com/ru/post/1269866/


All Articles