DATACMNS-1163 - EventPublishingInterceptor now prefers arguments over return values for event publication.

We now prefer to inspect the arguments handed to save(…) methods over inspecting the return value as the invocation of the actual method is free to exchange the instance handed into it and return a completely new one with the events contained in the parameter completely wiped.
This commit is contained in:
Oliver Gierke
2017-09-20 11:09:25 +02:00
parent a8cc4783bf
commit 72b927ea2c
2 changed files with 27 additions and 3 deletions

View File

@@ -235,12 +235,33 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
verify(secondEntity, times(1)).clearDomainEvents();
}
@Test // DATACMNS-1163
public void publishesEventFromParameter() throws Throwable {
Object event = new Object();
MultipleEvents parameter = MultipleEvents.of(Collections.singleton(event));
MultipleEvents returnValue = MultipleEvents.of(Collections.emptySet());
Method method = SampleRepository.class.getMethod("save", Object.class);
mockInvocation(invocation, method, parameter, returnValue);
EventPublishingMethodInterceptor.of(EventPublishingMethod.of(MultipleEvents.class), publisher).invoke(invocation);
verify(publisher, times(1)).publishEvent(event);
}
private static void mockInvocation(MethodInvocation invocation, Method method, Object parameterAndReturnValue)
throws Throwable {
mockInvocation(invocation, method, parameterAndReturnValue, parameterAndReturnValue);
}
private static void mockInvocation(MethodInvocation invocation, Method method, Object parameter, Object returnValue)
throws Throwable {
doReturn(method).when(invocation).getMethod();
doReturn(new Object[] { parameterAndReturnValue }).when(invocation).getArguments();
doReturn(parameterAndReturnValue).when(invocation).proceed();
doReturn(new Object[] { parameter }).when(invocation).getArguments();
doReturn(returnValue).when(invocation).proceed();
}
@Value(staticConstructor = "of")