OperationBatcher { public void operate() { if (closed) { throw new IllegalStateException("Batcher closed"); } if (++count < batchSize) { return; } operation(); reset(); count = 0; } OperationBatcher(int batchSize); void operate(); void close();  }
@Test public void batchSizeOfOneAlwaysPerformsOperation() { batcher = new TestBatcher(1); assertEquals(0, operationCount); assertEquals(1, resetCount); batcher.operate(); assertEquals(1, operationCount); assertEquals(2, resetCount); batcher.operate(); assertEquals(2, operationCount); assertEquals(3, resetCount); }
@Test public void batchSizeThree() { batcher = new TestBatcher(3); assertEquals(0, operationCount); assertEquals(1, resetCount); batcher.operate(); assertEquals(0, operationCount); assertEquals(1, resetCount); batcher.operate(); assertEquals(0, operationCount); assertEquals(1, resetCount); batcher.operate(); assertEquals(1, operationCount); assertEquals(2, resetCount); batcher.operate(); assertEquals(1, operationCount); assertEquals(2, resetCount); }