@Test public void testSpyRemovedOnClose() throws Exception { Spy spy = Sniffy.spy(); spy.close(); Sniffy.registeredSpies.stream(). filter(spyReference -> spyReference.get() == spy). forEach(spyReference -> fail("Spy was not removed from Sniffer observers")); }
spy.close(); } catch (SniffyAssertionError sniffyError) {
@Test public void testSpyClosed() throws Exception { Spy spy = Sniffer.execute(() -> { executeStatement(); Thread thread = new Thread(BaseTest::executeStatement); thread.start(); thread.join(); }).expectAtMostOnce(Threads.OTHERS); spy.close(); try { spy.verify(1); } catch (Spy.SpyClosedException e) { assertNotNull(e); assertNotNull(e.getCloseStackTrace()); assertNotNull(e.getCloseStackTrace()[1]); StackTraceElement stackTraceElement = e.getCloseStackTrace()[1]; assertEquals("io.sniffy.SnifferTest", stackTraceElement.getClassName()); assertEquals("testSpyClosed", stackTraceElement.getMethodName()); } }
public void afterInvocation(IInvokedMethod method, ITestResult testResult) { Object spyAttribute = testResult.getAttribute(SPY_ATTRIBUTE_NAME); if (null != spyAttribute) { Spy spy = (Spy) spyAttribute; try { spy.close(); } catch (SniffyAssertionError sniffyError) { testResult.setStatus(ITestResult.FAILURE); Throwable throwable = testResult.getThrowable(); if (null != throwable) { if (!ExceptionUtil.addSuppressed(throwable, sniffyError)) { sniffyError.printStackTrace(); } } else { throwable = sniffyError; } testResult.setThrowable(throwable); } testResult.removeAttribute(SPY_ATTRIBUTE_NAME); } Object disableSockets = testResult.getAttribute(DISABLE_SOCKETS_ATTRIBUTE_NAME); testResult.removeAttribute(DISABLE_SOCKETS_ATTRIBUTE_NAME); if (null != disableSockets) { ConnectionsRegistry.INSTANCE.clear(); } }
delegate.evaluate(); }).close(); } finally { if (null != disableSockets) {