DistributedLockServiceMySqlImpl implements DistributedLockService { synchronized boolean executeScalar(String operation, String key, Integer expectedResult) { int result = jdbcTemplate.queryForObject(operation, new Object[] { key }, Integer.class); return result == expectedResult; }  @Autowired(required = false) @Qualifier("distLockingDataSource") void setSmartDataSource(DataSource dataSource); @Override boolean tryLock(String key); @Override boolean tryReleaseLock(String key); void setJdbcTemplate(JdbcTemplate jdbcTemplate);  }
@Test public void testExecuteScalarExpected() { String query = "my query"; String key = "test"; Integer expectedResult = 0; when(service.jdbcTemplate.queryForObject(query, new Object[]{key},Integer.class)).thenReturn( expectedResult); assertTrue(service.executeScalar(query, key, expectedResult)); }
@Test public void testExecuteScalarUnexpected() { String query = "my query"; String key = "test"; Integer expectedResult = 0; when(service.jdbcTemplate.queryForObject(query, new Object[]{key},Integer.class)).thenReturn( expectedResult); assertFalse(service.executeScalar(query, key, expectedResult + 1)); }