public static DeploymentUnit mockDeploymentUnit() {
return mockDeploymentUnit("Mock Deployment Unit");
}
public static DeploymentUnit mockDeploymentUnit(String duName) {
final Attachable attachable = new SimpleAttachable();
final DeploymentUnit deploymentUnit = mock(DeploymentUnit.class);
when(deploymentUnit.getName()).thenReturn(duName);
when(deploymentUnit.getAttachment((AttachmentKey<Object>) any())).thenAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
AttachmentKey<?> key = (AttachmentKey<?>) invocation.getArguments()[0];
return attachable.getAttachment(key);
}
});
when(deploymentUnit.putAttachment((AttachmentKey<Object>) any(), any())).thenAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
AttachmentKey<Object> key = (AttachmentKey<Object>) invocation.getArguments()[0];
Object value = invocation.getArguments()[1];
return attachable.putAttachment(key, value);
}
});
ServiceName deploymentUnitServiceName = Services.deploymentUnitName(duName);
when(deploymentUnit.getServiceName()).thenReturn(deploymentUnitServiceName);
return deploymentUnit;