diff options
author | Klaudio Sinani <klaudio.sinani@sonarsource.com> | 2021-11-17 22:54:06 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-11-19 20:03:27 +0000 |
commit | a3d88ea27c35921647d7602755828ca73e15e865 (patch) | |
tree | 5626c38afab1ea00ab9897da431476c17b478bbe /server/sonar-auth-ldap/src/test | |
parent | 92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff) | |
download | sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip |
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'server/sonar-auth-ldap/src/test')
4 files changed, 29 insertions, 35 deletions
diff --git a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/CallbackHandlerImplTest.java b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/CallbackHandlerImplTest.java index 4129ab03855..7eca018edcf 100644 --- a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/CallbackHandlerImplTest.java +++ b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/CallbackHandlerImplTest.java @@ -26,6 +26,7 @@ import javax.security.auth.callback.UnsupportedCallbackException; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; public class CallbackHandlerImplTest { @@ -40,9 +41,12 @@ public class CallbackHandlerImplTest { assertThat(passwordCallback.getPassword()).isEqualTo("secret".toCharArray()); } - @Test(expected = UnsupportedCallbackException.class) - public void unsupportedCallback() throws Exception { - new CallbackHandlerImpl("tester", "secret").handle(new Callback[] {mock(Callback.class)}); + @Test + public void unsupportedCallback() { + assertThatThrownBy(() -> { + new CallbackHandlerImpl("tester", "secret").handle(new Callback[] {mock(Callback.class)}); + }) + .isInstanceOf(UnsupportedCallbackException.class); } } diff --git a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/ContextHelperTest.java b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/ContextHelperTest.java index 4e5c7ae2423..ffaf9613aed 100644 --- a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/ContextHelperTest.java +++ b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/ContextHelperTest.java @@ -23,6 +23,7 @@ import javax.naming.Context; import javax.naming.NamingException; import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -36,11 +37,12 @@ public class ContextHelperTest { ContextHelper.closeQuietly(context); } - @Test(expected = NamingException.class) + @Test public void shouldNotSwallow() throws Exception { Context context = mock(Context.class); doThrow(new NamingException()).when(context).close(); - ContextHelper.close(context, false); + assertThatThrownBy(() -> ContextHelper.close(context, false)) + .isInstanceOf(NamingException.class); } @Test diff --git a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSearchTest.java b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSearchTest.java index a19500cfb8c..367f0759a8b 100644 --- a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSearchTest.java +++ b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSearchTest.java @@ -26,21 +26,16 @@ import javax.naming.NamingException; import javax.naming.directory.SearchControls; import org.junit.BeforeClass; import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.auth.ldap.server.LdapServer; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class LdapSearchTest { @ClassRule public static LdapServer server = new LdapServer("/users.example.org.ldif"); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - private static Map<String, LdapContextFactory> contextFactories; @BeforeClass @@ -63,9 +58,10 @@ public class LdapSearchTest { assertThat(search.getReturningAttributes()).isEqualTo(new String[] {"objectClass"}); assertThat(search.toString()).isEqualTo("LdapSearch{baseDn=dc=example,dc=org, scope=subtree, request=(objectClass={0}), parameters=[inetOrgPerson], attributes=[objectClass]}"); assertThat(enumerationToArrayList(search.find()).size()).isEqualTo(3); - thrown.expect(NamingException.class); - thrown.expectMessage("Non unique result for " + search.toString()); - search.findUnique(); + + assertThatThrownBy(() -> search.findUnique()) + .isInstanceOf(NamingException.class) + .hasMessage("Non unique result for " + search.toString()); } @Test diff --git a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSettingsManagerTest.java b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSettingsManagerTest.java index f9927028341..446b09b3462 100644 --- a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSettingsManagerTest.java +++ b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapSettingsManagerTest.java @@ -21,30 +21,25 @@ package org.sonar.auth.ldap; import java.util.Arrays; import java.util.Collections; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.config.internal.MapSettings; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.auth.ldap.LdapAutodiscovery.LdapSrvRecord; public class LdapSettingsManagerTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void shouldFailWhenNoLdapUrl() { MapSettings settings = generateMultipleLdapSettingsWithUserAndGroupMapping(); settings.removeProperty("ldap.example.url"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()); - thrown.expect(LdapException.class); - thrown.expectMessage("The property 'ldap.example.url' property is empty while it is mandatory."); - settingsManager.getContextFactories(); + assertThatThrownBy(settingsManager::getContextFactories) + .isInstanceOf(LdapException.class) + .hasMessage("The property 'ldap.example.url' property is empty while it is mandatory."); } @Test @@ -53,11 +48,9 @@ public class LdapSettingsManagerTest { settings.setProperty("ldap.url", "ldap://foo"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()); - thrown.expect(LdapException.class); - thrown - .expectMessage( - "When defining multiple LDAP servers with the property 'ldap.servers', all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ..."); - settingsManager.getContextFactories(); + assertThatThrownBy(settingsManager::getContextFactories) + .isInstanceOf(LdapException.class) + .hasMessage("When defining multiple LDAP servers with the property 'ldap.servers', all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ..."); } @Test @@ -98,10 +91,9 @@ public class LdapSettingsManagerTest { LdapSettingsManager settingsManager = new LdapSettingsManager( generateAutodiscoverSettings().asConfig(), ldapAutodiscovery); - thrown.expect(LdapException.class); - thrown.expectMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server."); - - settingsManager.getContextFactories(); + assertThatThrownBy(settingsManager::getContextFactories) + .isInstanceOf(LdapException.class) + .hasMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server."); } /** @@ -139,9 +131,9 @@ public class LdapSettingsManagerTest { LdapSettingsManager settingsManager = new LdapSettingsManager( new MapSettings().asConfig(), new LdapAutodiscovery()); - thrown.expect(LdapException.class); - thrown.expectMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery."); - settingsManager.getContextFactories(); + assertThatThrownBy(settingsManager::getContextFactories) + .isInstanceOf(LdapException.class) + .hasMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery."); } private MapSettings generateMultipleLdapSettingsWithUserAndGroupMapping() { |