### Question:
LockFileHandler { public void createOrOverwriteLock(boolean overwriteLock) { synchronized(monitor) { try { if (overwriteLock) { hotfolder.deleteIfExists(lockUri); } boolean lockExists = hotfolder.exists(lockUri); if (lockExists) { throw new ConcurrentModificationException("Another client instance is running! See the lock file at " + lockUri); } writeLockFile(); } catch (IOException e) { logger.error("Error with server lock file: " + lockUri, e); } } }  void initConnection(String serverUrl, String user, String password); void createOrOverwriteLock(boolean overwriteLock); void deleteLockAndCleanUp();  }### Answer:
@Test public void shouldCreateNewLock() throws IOException { when(hotfolderMock.exists(lockUri)).thenReturn(false); when(hotfolderMock.createTmpFile(anyString())).thenReturn(new ByteArrayOutputStream()); lockHandlerSut.createOrOverwriteLock(false); verify(hotfolderMock, never()).deleteIfExists(lockUri); verify(hotfolderMock).copyTmpFile("lock", lockUri); }
@Test(expected=ConcurrentModificationException.class) public void shouldCancelWhenLockIsPresent() throws IOException { when(hotfolderMock.exists(lockUri)).thenReturn(true); lockHandlerSut.createOrOverwriteLock(false); }
@Test public void shouldOverwriteLock() throws IOException { when(hotfolderMock.exists(lockUri)).thenReturn(false); when(hotfolderMock.createTmpFile(anyString())).thenReturn(new ByteArrayOutputStream()); lockHandlerSut.createOrOverwriteLock(true); verify(hotfolderMock).deleteIfExists(lockUri); verify(hotfolderMock).copyTmpFile("lock", lockUri); }