### Question:
Configuration { public DirContext buildContextConnection() throws NamingException { if (isValidConfiguration()) { try { return new InitialDirContext(settings); } catch (NamingException e) { logger.error(e.getMessage(), e); throw e; } } throw new IllegalStateException("Can't build connection because settings are not valid"); } @Inject @VisibleForTesting  Configuration(IniFile.Factory iniFileFactory,
			@Named(LoggerModule.CONFIGURATION) Logger configurationLogger); DirContext buildContextConnection(); String getUrl(); String getBaseDn(); String getFilter(); int getLimit(); void cleanup(DirContext ctx); boolean isValidConfiguration();  }### Answer:
@Test(expected=IllegalStateException.class) public void testBuildContextConnectionFailsIfUrlIsMissing() throws Exception { expect(iniFile.getStringValue(SEARCH_LDAP_URL)).andReturn(null); expect(iniFile.getStringValue(SEARCH_LDAP_BASE)).andReturn("%d,dc=local"); expect(iniFile.getStringValue(SEARCH_LDAP_FILTER)).andReturn("filter"); expect(iniFile.getIntegerValue(SEARCH_LDAP_LIMIT, null)).andReturn(5); mocks.replay(); new Configuration(iniFileFactory, logger).buildContextConnection(); }
@Test(expected=IllegalStateException.class) public void testBuildContextConnectionFailsIfBaseIsMissing() throws Exception { expect(iniFile.getStringValue(SEARCH_LDAP_URL)).andReturn("ldaps: expect(iniFile.getStringValue(SEARCH_LDAP_BASE)).andReturn(null); expect(iniFile.getStringValue(SEARCH_LDAP_FILTER)).andReturn("filter"); expect(iniFile.getIntegerValue(SEARCH_LDAP_LIMIT, null)).andReturn(5); mocks.replay(); new Configuration(iniFileFactory, logger).buildContextConnection(); }
@Test(expected=IllegalStateException.class) public void testBuildContextConnectionFailsIfFilterIsMissing() throws Exception { expect(iniFile.getStringValue(SEARCH_LDAP_URL)).andReturn("ldaps: expect(iniFile.getStringValue(SEARCH_LDAP_BASE)).andReturn("%d,dc=local"); expect(iniFile.getStringValue(SEARCH_LDAP_FILTER)).andReturn(null); expect(iniFile.getIntegerValue(SEARCH_LDAP_LIMIT, null)).andReturn(5); mocks.replay(); new Configuration(iniFileFactory, logger).buildContextConnection(); }
@Ignore("A connection toward the url tries to be done") @Test public void testBuildContextConnection() throws Exception { expect(iniFile.getStringValue(SEARCH_LDAP_URL)).andReturn("ldaps: expect(iniFile.getStringValue(SEARCH_LDAP_BASE)).andReturn("%d,dc=local"); expect(iniFile.getStringValue(SEARCH_LDAP_FILTER)).andReturn("filter"); expect(iniFile.getIntegerValue(SEARCH_LDAP_LIMIT, null)).andReturn(5); mocks.replay(); DirContext connection = new Configuration(iniFileFactory, logger).buildContextConnection(); mocks.verify(); assertThat(connection).isNotNull(); }