### Question:
StatementLocksFactorySelector { public StatementLocksFactory select() { StatementLocksFactory statementLocksFactory; String serviceName = StatementLocksFactory.class.getSimpleName(); List<StatementLocksFactory> factories = serviceLoadFactories(); if ( factories.isEmpty() ) { statementLocksFactory = new SimpleStatementLocksFactory(); log.info( "No services implementing " + serviceName + " found. " + "Using " + SimpleStatementLocksFactory.class.getSimpleName() ); } else if ( factories.size() == 1 ) { statementLocksFactory = factories.get( 0 ); log.info( "Found single implementation of " + serviceName + ". Namely " + statementLocksFactory.getClass().getSimpleName() ); } else { throw new IllegalStateException( "Found more than one implementation of " + serviceName + ": " + factories ); } statementLocksFactory.initialize( locks, config ); return statementLocksFactory; } StatementLocksFactorySelector( Locks locks, Config config, LogService logService ); StatementLocksFactory select();  }### Answer:
@Test public void loadSimpleStatementLocksFactoryWhenNoServices() { Locks locks = mock( Locks.class ); Locks.Client locksClient = mock( Client.class ); when( locks.newClient() ).thenReturn( locksClient ); StatementLocksFactorySelector loader = newLoader( locks ); StatementLocksFactory factory = loader.select(); StatementLocks statementLocks = factory.newInstance(); assertThat( factory, instanceOf( SimpleStatementLocksFactory.class ) ); assertThat( statementLocks, instanceOf( SimpleStatementLocks.class ) ); assertSame( locksClient, statementLocks.optimistic() ); assertSame( locksClient, statementLocks.pessimistic() ); }
@Test public void loadSingleAvailableFactory() { Locks locks = mock( Locks.class ); StatementLocksFactory factory = mock( StatementLocksFactory.class ); StatementLocksFactorySelector loader = newLoader( locks, factory ); StatementLocksFactory loadedFactory = loader.select(); assertSame( factory, loadedFactory ); verify( factory ).initialize( same( locks ), any( Config.class ) ); }
@Test public void throwWhenMultipleFactoriesLoaded() { TestStatementLocksFactorySelector loader = newLoader( mock( Locks.class ), mock( StatementLocksFactory.class ), mock( StatementLocksFactory.class ), mock( StatementLocksFactory.class ) ); try { loader.select(); } catch ( Exception e ) { assertThat( e, instanceOf( IllegalStateException.class ) ); } }