ConnectionPartition implements Serializable { protected void updateCreatedConnections(int increment) { try{ this.statsLock.writeLock().lock(); this.createdConnections+=increment; } finally { this.statsLock.writeLock().unlock(); } } ConnectionPartition(BoneCP pool); int getRemainingCapacity(); @Override String toString();  }
@Test public void testUpdateCreatedConnections() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { int count = testClass.getCreatedConnections(); testClass.updateCreatedConnections(5); assertEquals(count+5, testClass.getCreatedConnections()); }
@Test public void testUpdateCreatedConnectionsWithException() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { ReentrantReadWriteLock mockLock = createNiceMock(ReentrantReadWriteLock.class); WriteLock mockWriteLock = createNiceMock(WriteLock.class); Field field = testClass.getClass().getDeclaredField("statsLock"); field.setAccessible(true); ReentrantReadWriteLock oldLock = (ReentrantReadWriteLock) field.get(testClass); field.set(testClass, mockLock); expect(mockLock.writeLock()).andThrow(new RuntimeException()).once().andReturn(mockWriteLock).once(); mockWriteLock.lock(); expectLastCall().once(); replay(mockLock, mockWriteLock); try{ testClass.updateCreatedConnections(5); fail("Should have thrown an exception"); } catch (Throwable t){ } verify(mockLock); field.set(testClass, oldLock); }