@Test
public void testPublishErrorNotification() throws Exception {

    final EndpointDescription epd = EasyMock.createNiceMock(EndpointDescription.class);
    EasyMock.expect(epd.getInterfaces()).andReturn(Arrays.asList("org.foo.Bar")).anyTimes();
    EasyMock.replay(epd);
    final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class);
    EasyMock.replay(sref);
    final ExportReference er = EasyMock.createNiceMock(ExportReference.class);
    EasyMock.expect(er.getExportedEndpoint()).andReturn(epd).anyTimes();
    EasyMock.expect(er.getExportedService()).andReturn(sref).anyTimes();
    EasyMock.replay(er);
    final Bundle bundle = EasyMock.createNiceMock(Bundle.class);
    EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes();
    EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes();
    EasyMock.expect(bundle.getHeaders()).andReturn(new Hashtable<>()).anyTimes();
    EasyMock.replay(bundle);
    final Exception exportException = new Exception();
    EventAdmin ea = EasyMock.createNiceMock(EventAdmin.class);
    ea.postEvent(EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
        @Override
        public Object answer() throws Throwable {
            Event event = (Event) EasyMock.getCurrentArguments()[0];
            Assert.assertEquals("org/osgi/service/remoteserviceadmin/EXPORT_ERROR", event.getTopic());
            Assert.assertSame(bundle, event.getProperty("bundle"));
            Assert.assertEquals(42L, event.getProperty("bundle.id"));
            Assert.assertEquals("test.bundle", event.getProperty("bundle.symbolicname"));
            Assert.assertEquals(new Version("0"), event.getProperty("bundle.version"));
            Assert.assertSame(exportException, event.getProperty("cause"));
            Assert.assertEquals(epd, event.getProperty("export.registration"));
            Assert.assertTrue(Arrays.equals(new String[] {"org.foo.Bar"},
                                            (String[]) event.getProperty("objectClass")));
            RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) event.getProperty("event");
            Assert.assertSame(exportException, rsae.getException());
            Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_ERROR, rsae.getType());
            Assert.assertSame(bundle, rsae.getSource());
            ExportReference er = rsae.getExportReference();
            Assert.assertSame(epd, er.getExportedEndpoint());
            Assert.assertSame(sref, er.getExportedService());
            return null;
        }
    });
    EasyMock.replay(ea);
    ServiceReference eaSref = EasyMock.createNiceMock(ServiceReference.class);
    EasyMock.replay(eaSref);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes();
    EasyMock.expect(bc.getAllServiceReferences(EventAdmin.class.getName(), null))
        .andReturn(new ServiceReference[] {eaSref}).anyTimes();
    EasyMock.expect(bc.getService(eaSref)).andReturn(ea).anyTimes();
    EasyMock.replay(bc);
    RemoteServiceAdminEvent event = new RemoteServiceAdminEvent(
            RemoteServiceAdminEvent.EXPORT_ERROR,
            bundle,
            er,
            exportException
    );
    new EventAdminHelper(bc).remoteAdminEvent(event);
    EasyMock.verify(epd, sref, er, ea, eaSref, bc);
}