aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>2021-11-17 22:54:06 +0100
committersonartech <sonartech@sonarsource.com>2021-11-19 20:03:27 +0000
commita3d88ea27c35921647d7602755828ca73e15e865 (patch)
tree5626c38afab1ea00ab9897da431476c17b478bbe /sonar-core/src
parent92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff)
downloadsonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz
sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java27
-rw-r--r--sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionRepositoryImplTest.java54
-rw-r--r--sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionsLoaderTest.java19
-rw-r--r--sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java19
-rw-r--r--sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java12
-rw-r--r--sonar-core/src/test/java/org/sonar/core/i18n/I18nClassloaderTest.java10
-rw-r--r--sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/platform/ComponentContainerTest.java29
-rw-r--r--sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java64
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java57
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java42
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/LineReaderIteratorTest.java20
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java22
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java36
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java245
15 files changed, 273 insertions, 387 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
index 7206a9be3d9..a97e60244ae 100644
--- a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
@@ -19,15 +19,12 @@
*/
package org.sonar.core.component;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class ComponentKeysTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void create_key_from_module_key_path_and_branch() {
@@ -64,30 +61,26 @@ public class ComponentKeysTest {
@Test
public void checkProjectKey_fail_if_key_is_empty() {
- expectedException.expect(IllegalArgumentException.class);
-
- ComponentKeys.checkProjectKey("");
+ assertThatThrownBy(() -> ComponentKeys.checkProjectKey(""))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void checkProjectKey_fail_if_space() {
- expectedException.expect(IllegalArgumentException.class);
-
- ComponentKeys.checkProjectKey("ab 12");
+ assertThatThrownBy(() -> ComponentKeys.checkProjectKey("ab 12"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void checkProjectKey_fail_if_only_digit() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Malformed key for '0123'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
-
- ComponentKeys.checkProjectKey("0123");
+ assertThatThrownBy(() -> ComponentKeys.checkProjectKey("0123"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Malformed key for '0123'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
}
@Test
public void checkProjectKey_fail_if_special_characters_not_allowed() {
- expectedException.expect(IllegalArgumentException.class);
-
- ComponentKeys.checkProjectKey("ab/12");
+ assertThatThrownBy(() -> ComponentKeys.checkProjectKey("0123"))
+ .isInstanceOf(IllegalArgumentException.class);
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionRepositoryImplTest.java b/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionRepositoryImplTest.java
index a457541f4ca..7b0520b0bbe 100644
--- a/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionRepositoryImplTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionRepositoryImplTest.java
@@ -25,27 +25,24 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.Set;
import java.util.stream.Collectors;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
@RunWith(DataProviderRunner.class)
public class CoreExtensionRepositoryImplTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private CoreExtensionRepositoryImpl underTest = new CoreExtensionRepositoryImpl();
@Test
public void loadedCoreExtensions_fails_with_ISE_if_called_before_setLoadedCoreExtensions() {
- expectRepositoryNotInitializedISE();
-
- underTest.loadedCoreExtensions();
+ assertThatThrownBy(() -> underTest.loadedCoreExtensions())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Repository has not been initialized yet");
}
@Test
@@ -59,9 +56,8 @@ public class CoreExtensionRepositoryImplTest {
@Test
public void setLoadedCoreExtensions_fails_with_NPE_if_argument_is_null() {
- expectedException.expect(NullPointerException.class);
-
- underTest.setLoadedCoreExtensions(null);
+ assertThatThrownBy(() -> underTest.setLoadedCoreExtensions(null))
+ .isInstanceOf(NullPointerException.class);
}
@Test
@@ -69,19 +65,18 @@ public class CoreExtensionRepositoryImplTest {
public void setLoadedCoreExtensions_fails_with_ISE_if_called_twice(Set<CoreExtension> coreExtensions) {
underTest.setLoadedCoreExtensions(coreExtensions);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Repository has already been initialized");
-
- underTest.setLoadedCoreExtensions(coreExtensions);
+ assertThatThrownBy(() -> underTest.setLoadedCoreExtensions(coreExtensions))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Repository has already been initialized");
}
@Test
public void installed_fails_with_ISE_if_called_before_setLoadedCoreExtensions() {
CoreExtension coreExtension = newCoreExtension();
- expectRepositoryNotInitializedISE();
-
- underTest.installed(coreExtension);
+ assertThatThrownBy(() -> underTest.installed(coreExtension))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Repository has not been initialized yet");
}
@Test
@@ -90,10 +85,9 @@ public class CoreExtensionRepositoryImplTest {
underTest.setLoadedCoreExtensions(coreExtensions);
CoreExtension coreExtension = newCoreExtension();
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Specified CoreExtension has not been loaded first");
-
- underTest.installed(coreExtension);
+ assertThatThrownBy(() -> underTest.installed(coreExtension))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Specified CoreExtension has not been loaded first");
}
@Test
@@ -101,17 +95,16 @@ public class CoreExtensionRepositoryImplTest {
public void installed_fails_with_NPE_if_CoreExtension_is_null(Set<CoreExtension> coreExtensions) {
underTest.setLoadedCoreExtensions(coreExtensions);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("coreExtension can't be null");
-
- underTest.installed(null);
+ assertThatThrownBy(() -> underTest.installed(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("coreExtension can't be null");
}
@Test
public void isInstalled_fails_with_ISE_if_called_before_setLoadedCoreExtensions() {
- expectRepositoryNotInitializedISE();
-
- underTest.isInstalled("foo");
+ assertThatThrownBy(() -> underTest.isInstalled("foo"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Repository has not been initialized yet");
}
@Test
@@ -139,11 +132,6 @@ public class CoreExtensionRepositoryImplTest {
assertThat(underTest.isInstalled(coreExtension.getName())).isTrue();
}
- private void expectRepositoryNotInitializedISE() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Repository has not been initialized yet");
- }
-
@DataProvider
public static Object[][] coreExtensionsSets() {
return new Object[][] {
diff --git a/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionsLoaderTest.java b/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionsLoaderTest.java
index edbbe9a1480..a1f13b5d046 100644
--- a/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionsLoaderTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/extension/CoreExtensionsLoaderTest.java
@@ -25,19 +25,16 @@ import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class CoreExtensionsLoaderTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
private ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class);
@@ -73,10 +70,9 @@ public class CoreExtensionsLoaderTest {
Set<CoreExtension> coreExtensions = ImmutableSet.of(newCoreExtension("a"), newCoreExtension("a"));
when(serviceLoaderWrapper.load(any())).thenReturn(coreExtensions);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Multiple core extensions declare the following names: a");
-
- underTest.load();
+ assertThatThrownBy(() -> underTest.load())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Multiple core extensions declare the following names: a");
}
@Test
@@ -84,10 +80,9 @@ public class CoreExtensionsLoaderTest {
Set<CoreExtension> coreExtensions = ImmutableSet.of(newCoreExtension("a"), newCoreExtension("a"), newCoreExtension("b"), newCoreExtension("b"));
when(serviceLoaderWrapper.load(any())).thenReturn(coreExtensions);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Multiple core extensions declare the following names: a, b");
-
- underTest.load();
+ assertThatThrownBy(() -> underTest.load())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Multiple core extensions declare the following names: a, b");
}
private static CoreExtension newCoreExtension(String name) {
diff --git a/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java b/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java
index 29c0dea8387..08bfc56bbad 100644
--- a/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java
@@ -19,28 +19,25 @@
*/
package org.sonar.core.hash;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class LineRangeTest {
- @Rule
- public ExpectedException exception = ExpectedException.none();
@Test
public void should_throw_ISE_if_range_is_invalid() {
- exception.expect(IllegalArgumentException.class);
- exception.expectMessage("Line range is not valid: 1 must be greater or equal than 2");
- new LineRange(2, 1);
+ assertThatThrownBy(() -> new LineRange(2, 1))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Line range is not valid: 1 must be greater or equal than 2");
}
-
+
@Test
public void should_throw_ISE_if_startOffset_is_invalid() {
- exception.expect(IllegalArgumentException.class);
- exception.expectMessage("Start offset not valid: -1");
- new LineRange(-1, 1);
+ assertThatThrownBy(() -> new LineRange(-1, 1))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Start offset not valid: -1");
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java b/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java
index c92490e9320..0fa8e152b52 100644
--- a/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java
@@ -22,22 +22,18 @@ package org.sonar.core.hash;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class SourceLinesHashesComputerTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void addLine_throws_NPE_is_line_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("line can not be null");
-
- new SourceLineHashesComputer(1).addLine(null);
+ assertThatThrownBy(() -> new SourceLineHashesComputer(1).addLine(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("line can not be null");
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/i18n/I18nClassloaderTest.java b/sonar-core/src/test/java/org/sonar/core/i18n/I18nClassloaderTest.java
index 6f54de151a6..578b9bd1cf6 100644
--- a/sonar-core/src/test/java/org/sonar/core/i18n/I18nClassloaderTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/i18n/I18nClassloaderTest.java
@@ -23,20 +23,16 @@ import com.google.common.collect.Lists;
import java.net.URL;
import java.net.URLClassLoader;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.core.platform.PluginRepository;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
public class I18nClassloaderTest {
private I18nClassloader i18nClassloader;
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Before
public void init() {
i18nClassloader = new I18nClassloader(mock(PluginRepository.class));
@@ -63,8 +59,8 @@ public class I18nClassloaderTest {
@Test
public void not_support_lookup_of_java_classes() throws ClassNotFoundException {
- thrown.expect(UnsupportedOperationException.class);
- i18nClassloader.loadClass("java.lang.String");
+ assertThatThrownBy(() -> i18nClassloader.loadClass("java.lang.String"))
+ .isInstanceOf(UnsupportedOperationException.class);
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java b/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java
index 61d8511f519..29c6533af59 100644
--- a/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java
@@ -26,9 +26,7 @@ import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import static java.util.Arrays.asList;
@@ -44,8 +42,6 @@ public class TrackerTest {
public static final RuleKey RULE_USE_DIAMOND = RuleKey.of("java", "UseDiamond");
public static final RuleKey RULE_MISSING_PACKAGE_INFO = RuleKey.of("java", "MissingPackageInfo");
- @Rule
- public ExpectedException thrown = ExpectedException.none();
Tracker<Issue, Issue> tracker = new Tracker<>();
diff --git a/sonar-core/src/test/java/org/sonar/core/platform/ComponentContainerTest.java b/sonar-core/src/test/java/org/sonar/core/platform/ComponentContainerTest.java
index 9c415c7a7f1..5598f5fa3e2 100644
--- a/sonar-core/src/test/java/org/sonar/core/platform/ComponentContainerTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/platform/ComponentContainerTest.java
@@ -23,9 +23,7 @@ import java.io.Closeable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.picocontainer.Startable;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.Property;
@@ -34,14 +32,13 @@ import org.sonar.api.config.PropertyDefinitions;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
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.spy;
import static org.mockito.Mockito.verify;
public class ComponentContainerTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldRegisterItself() {
@@ -267,8 +264,8 @@ public class ComponentContainerTest {
container.add(UnstartableComponent.class);
// do not expect a PicoException
- thrown.expect(IllegalStateException.class);
- container.startComponents();
+ assertThatThrownBy(container::startComponents)
+ .isInstanceOf(IllegalStateException.class);
}
@Test
@@ -278,11 +275,9 @@ public class ComponentContainerTest {
container.startComponents();
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Unable to register extension org.sonar.core.platform.ComponentContainerTest$UnstartableComponent");
-
- container.addExtension(plugin, UnstartableComponent.class);
-
+ assertThatThrownBy(() -> container.addExtension(plugin, UnstartableComponent.class))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Unable to register extension org.sonar.core.platform.ComponentContainerTest$UnstartableComponent");
}
@Test
@@ -340,9 +335,9 @@ public class ComponentContainerTest {
ComponentContainer container = new ComponentContainer();
container.add(UnstartableComponent.class, FailingStopWithISEComponent.class);
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Fail to start");
- container.execute();
+ assertThatThrownBy(container::execute)
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Fail to start");
}
@Test
@@ -350,9 +345,9 @@ public class ComponentContainerTest {
ComponentContainer container = new ComponentContainer();
container.add(UnstartableApiComponent.class, FailingStopWithISEComponent.class);
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Fail to start");
- container.execute();
+ assertThatThrownBy(container::execute)
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Fail to start");
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java b/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java
index a02b0120bdd..bf57622c075 100644
--- a/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java
@@ -27,15 +27,14 @@ import java.util.Date;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.sonar.core.util.UuidFactoryImpl;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang.StringUtils.repeat;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.core.platform.ServerId.DATABASE_ID_LENGTH;
import static org.sonar.core.platform.ServerId.DEPRECATED_SERVER_ID_LENGTH;
import static org.sonar.core.platform.ServerId.NOT_UUID_DATASET_ID_LENGTH;
@@ -47,23 +46,19 @@ import static org.sonar.core.platform.ServerId.Format.WITH_DATABASE_ID;
@RunWith(DataProviderRunner.class)
public class ServerIdTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void parse_throws_NPE_if_argument_is_null() {
- expectedException.expect(NullPointerException.class);
-
- ServerId.parse(null);
+ assertThatThrownBy(() -> ServerId.parse(null))
+ .isInstanceOf(NullPointerException.class);
}
@Test
@UseDataProvider("emptyAfterTrim")
public void parse_throws_IAE_if_parameter_is_empty_after_trim(String serverId) {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("serverId can't be empty");
-
- ServerId.parse(serverId);
+ assertThatThrownBy(() -> ServerId.parse(serverId))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("serverId can't be empty");
}
@DataProvider
@@ -78,10 +73,9 @@ public class ServerIdTest {
@Test
@UseDataProvider("wrongFormatWithDatabaseId")
public void parse_throws_IAE_if_split_char_is_at_wrong_position(String emptyDatabaseId) {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Unrecognized serverId format. Parts have wrong length");
-
- ServerId.parse(emptyDatabaseId);
+ assertThatThrownBy(() -> ServerId.parse(emptyDatabaseId))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Unrecognized serverId format. Parts have wrong length");
}
@DataProvider
@@ -170,33 +164,29 @@ public class ServerIdTest {
@Test
public void parse_does_not_support_deprecated_server_id_with_database_id() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("serverId does not have a supported length");
-
- ServerId.parse(randomAlphabetic(DATABASE_ID_LENGTH) + SPLIT_CHARACTER + randomAlphabetic(DEPRECATED_SERVER_ID_LENGTH));
+ assertThatThrownBy(() -> ServerId.parse(randomAlphabetic(DATABASE_ID_LENGTH) + SPLIT_CHARACTER + randomAlphabetic(DEPRECATED_SERVER_ID_LENGTH)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("serverId does not have a supported length");
}
@Test
public void of_throws_NPE_if_datasetId_is_null() {
- expectedException.expect(NullPointerException.class);
-
- ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), null);
+ assertThatThrownBy(() -> ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), null))
+ .isInstanceOf(NullPointerException.class);
}
@Test
public void of_throws_IAE_if_datasetId_is_empty() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Illegal datasetId length (0)");
-
- ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), "");
+ assertThatThrownBy(() -> ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), ""))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Illegal datasetId length (0)");
}
@Test
public void of_throws_IAE_if_databaseId_is_empty() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Illegal databaseId length (0)");
-
- ServerId.of("", randomAlphabetic(UUID_DATASET_ID_LENGTH));
+ assertThatThrownBy(() -> ServerId.of("", randomAlphabetic(UUID_DATASET_ID_LENGTH)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Illegal databaseId length (0)");
}
@Test
@@ -215,10 +205,9 @@ public class ServerIdTest {
String databaseId = randomAlphabetic(illegalDatabaseIdLengths);
String datasetId = randomAlphabetic(UUID_DATASET_ID_LENGTH);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Illegal databaseId length (" + illegalDatabaseIdLengths + ")");
-
- ServerId.of(databaseId, datasetId);
+ assertThatThrownBy(() -> ServerId.of(databaseId, datasetId))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Illegal databaseId length (" + illegalDatabaseIdLengths + ")");
}
@DataProvider
@@ -235,10 +224,9 @@ public class ServerIdTest {
String datasetId = randomAlphabetic(illegalDatasetIdLengths);
String databaseId = randomAlphabetic(DATABASE_ID_LENGTH);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Illegal datasetId length (" + illegalDatasetIdLengths + ")");
-
- ServerId.of(databaseId, datasetId);
+ assertThatThrownBy(() -> ServerId.of(databaseId, datasetId))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Illegal datasetId length (" + illegalDatasetIdLengths + ")");
}
@DataProvider
diff --git a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java
index d313263982e..d07e489dff2 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java
@@ -23,12 +23,10 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
-import java.net.NoRouteToHostException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
-import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -43,7 +41,6 @@ import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.DisableOnDebug;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
@@ -57,7 +54,7 @@ import org.sonar.api.platform.Server;
import org.sonar.api.utils.SonarException;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
@@ -69,8 +66,6 @@ public class DefaultHttpDownloaderTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Rule
public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
@@ -133,23 +128,21 @@ public class DefaultHttpDownloaderTest {
// non routable address
String url = "http://10.255.255.1";
- thrown.expect(SonarException.class);
- thrown.expect(hasCause(new BaseMatcher<Exception>() {
- @Override
- public boolean matches(Object ex) {
- return
- // Java 8
- ex instanceof NoRouteToHostException || ex instanceof SocketException
- // Java 7 or before
- || ex instanceof SocketTimeoutException;
- }
+ assertThatThrownBy(() -> {
+ DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings().asConfig(), 10, 50000);
+ downloader.openStream(new URI(url));
+ })
+ .isInstanceOf(SonarException.class)
+ .isEqualToComparingFieldByField(new BaseMatcher<Exception>() {
+ @Override
+ public boolean matches(Object ex) {
+ return ex instanceof SonarException && ((SonarException) ex).getCause() instanceof SocketTimeoutException;
+ }
- @Override
- public void describeTo(Description arg0) {
- }
- }));
- DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings().asConfig(), 10, 50000);
- downloader.openStream(new URI(url));
+ @Override
+ public void describeTo(Description arg0) {
+ }
+ });
}
@Test
@@ -178,17 +171,17 @@ public class DefaultHttpDownloaderTest {
@Test
public void readStringWithTimeout() throws URISyntaxException {
- thrown.expect(new BaseMatcher<Exception>() {
- @Override
- public boolean matches(Object ex) {
- return ex instanceof SonarException && ((SonarException) ex).getCause() instanceof SocketTimeoutException;
- }
+ assertThatThrownBy(() -> new DefaultHttpDownloader(new MapSettings().asConfig(), 50).readString(new URI(baseUrl + "/timeout/"), StandardCharsets.UTF_8))
+ .isEqualToComparingFieldByField(new BaseMatcher<Exception>() {
+ @Override
+ public boolean matches(Object ex) {
+ return ex instanceof SonarException && ((SonarException) ex).getCause() instanceof SocketTimeoutException;
+ }
- @Override
- public void describeTo(Description arg0) {
- }
- });
- new DefaultHttpDownloader(new MapSettings().asConfig(), 50).readString(new URI(baseUrl + "/timeout/"), StandardCharsets.UTF_8);
+ @Override
+ public void describeTo(Description arg0) {
+ }
+ });
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java
index de333333ccb..1cc07126f36 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java
@@ -28,23 +28,21 @@ import javax.annotation.CheckForNull;
import org.apache.commons.lang.SystemUtils;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assume.assumeTrue;
public class FileUtilsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void cleanDirectory_throws_NPE_if_file_is_null() throws IOException {
- expectDirectoryCanNotBeNullNPE();
-
- FileUtils.cleanDirectory(null);
+ assertThatThrownBy(() -> FileUtils.cleanDirectory(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Directory can not be null");
}
@Test
@@ -56,10 +54,9 @@ public class FileUtilsTest {
public void cleanDirectory_throws_IAE_if_argument_is_a_file() throws IOException {
File file = temporaryFolder.newFile();
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'" + file.getAbsolutePath() + "' is not a directory");
-
- FileUtils.cleanDirectory(file);
+ assertThatThrownBy(() -> FileUtils.cleanDirectory(file))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("'" + file.getAbsolutePath() + "' is not a directory");
}
@Test
@@ -202,9 +199,9 @@ public class FileUtilsTest {
@Test
public void deleteDirectory_throws_NPE_if_argument_is_null() throws IOException {
- expectDirectoryCanNotBeNullNPE();
-
- FileUtils.deleteDirectory((File) null);
+ assertThatThrownBy(() -> FileUtils.deleteDirectory((File) null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Directory can not be null");
}
@Test
@@ -218,10 +215,9 @@ public class FileUtilsTest {
public void deleteDirectory_throws_IOE_if_argument_is_a_file() throws IOException {
File file = temporaryFolder.newFile();
- expectedException.expect(IOException.class);
- expectedException.expectMessage("Directory '" + file.getAbsolutePath() + "' is a file");
-
- FileUtils.deleteDirectory(file);
+ assertThatThrownBy(() -> FileUtils.deleteDirectory(file))
+ .isInstanceOf(IOException.class)
+ .hasMessage("Directory '" + file.getAbsolutePath() + "' is a file");
}
@Test
@@ -234,10 +230,9 @@ public class FileUtilsTest {
assertThat(file1).isRegularFile();
assertThat(symLink).isSymbolicLink();
- expectedException.expect(IOException.class);
- expectedException.expectMessage("Directory '" + symLink.toFile().getAbsolutePath() + "' is a symbolic link");
-
- FileUtils.deleteDirectory(symLink.toFile());
+ assertThatThrownBy(() -> FileUtils.deleteDirectory(symLink.toFile()))
+ .isInstanceOf(IOException.class)
+ .hasMessage("Directory '" + symLink.toFile().getAbsolutePath() + "' is a symbolic link");
}
@Test
@@ -263,11 +258,6 @@ public class FileUtilsTest {
assertThat(childDir2).doesNotExist();
}
- private void expectDirectoryCanNotBeNullNPE() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Directory can not be null");
- }
-
@CheckForNull
private static Object getFileKey(Path path) throws IOException {
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
diff --git a/sonar-core/src/test/java/org/sonar/core/util/LineReaderIteratorTest.java b/sonar-core/src/test/java/org/sonar/core/util/LineReaderIteratorTest.java
index f83745259bc..4153d9b164d 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/LineReaderIteratorTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/LineReaderIteratorTest.java
@@ -22,18 +22,15 @@ package org.sonar.core.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
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;
public class LineReaderIteratorTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void read_lines() {
@@ -71,13 +68,14 @@ public class LineReaderIteratorTest {
@Test
public void fail_if_cannot_read() throws IOException {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Fail to read line");
+ assertThatThrownBy(() -> {
+ BufferedReader reader = mock(BufferedReader.class);
+ when(reader.readLine()).thenThrow(new IOException());
+ LineReaderIterator it = new LineReaderIterator(reader);
- BufferedReader reader = mock(BufferedReader.class);
- when(reader.readLine()).thenThrow(new IOException());
- LineReaderIterator it = new LineReaderIterator(reader);
-
- it.hasNext();
+ it.hasNext();
+ })
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Fail to read line");
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java
index da1f7ff7136..d83b5781fc2 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java
@@ -21,9 +21,7 @@ package org.sonar.core.util;
import com.google.protobuf.ByteString;
import java.io.StringWriter;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.core.test.Test.Countries;
import org.sonar.core.test.Test.Country;
@@ -39,12 +37,11 @@ import org.sonar.core.test.Test.Translations;
import org.sonar.test.TestUtils;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.core.util.ProtobufJsonFormat.toJson;
public class ProtobufJsonFormatTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void test_primitive_types() {
@@ -63,14 +60,15 @@ public class ProtobufJsonFormatTest {
@Test
public void bytes_field_can_not_be_converted() {
- expectedException.expect(RuntimeException.class);
- expectedException.expectMessage("JSON format does not support type 'BYTE_STRING' of field 'bytesField'");
-
- PrimitiveTypeMsg protobuf = PrimitiveTypeMsg.newBuilder()
- .setBytesField(ByteString.copyFrom(new byte[]{2, 4}))
- .build();
-
- ProtobufJsonFormat.write(protobuf, JsonWriter.of(new StringWriter()));
+ assertThatThrownBy(() -> {
+ PrimitiveTypeMsg protobuf = PrimitiveTypeMsg.newBuilder()
+ .setBytesField(ByteString.copyFrom(new byte[]{2, 4}))
+ .build();
+
+ ProtobufJsonFormat.write(protobuf, JsonWriter.of(new StringWriter()));
+ })
+ .isInstanceOf(RuntimeException.class)
+ .hasMessage("JSON format does not support type 'BYTE_STRING' of field 'bytesField'");
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java
index 93f8f9d7b66..73bc718b8c0 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java
@@ -23,18 +23,16 @@ import java.io.File;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.test.TestUtils;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.core.test.Test.Fake;
public class ProtobufTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@@ -46,12 +44,12 @@ public class ProtobufTest {
@Test
public void read_file_fails_if_file_does_not_exist() throws Exception {
- thrown.expect(ContextException.class);
- thrown.expectMessage("Unable to read message");
-
- File file = temp.newFile();
- FileUtils.forceDelete(file);
- Protobuf.read(file, Fake.parser());
+ assertThatThrownBy(() -> {
+ File file = temp.newFile();
+ FileUtils.forceDelete(file);
+ Protobuf.read(file, Fake.parser());
+ }).isInstanceOf(ContextException.class)
+ .hasMessageContaining("Unable to read message");
}
@Test
@@ -73,11 +71,11 @@ public class ProtobufTest {
@Test
public void fail_to_write_single_message() throws Exception {
- thrown.expect(ContextException.class);
- thrown.expectMessage("Unable to write message");
-
- File dir = temp.newFolder();
- Protobuf.write(Fake.getDefaultInstance(), dir);
+ assertThatThrownBy(() -> {
+ File dir = temp.newFolder();
+ Protobuf.write(Fake.getDefaultInstance(), dir);
+ }).isInstanceOf(ContextException.class)
+ .hasMessageContaining("Unable to write message");
}
@Test
@@ -100,11 +98,11 @@ public class ProtobufTest {
@Test
public void fail_to_read_stream() throws Exception {
- thrown.expect(ContextException.class);
- thrown.expectMessage("Unable to read messages");
-
- File dir = temp.newFolder();
- Protobuf.readStream(dir, Fake.parser());
+ assertThatThrownBy(() -> {
+ File dir = temp.newFolder();
+ Protobuf.readStream(dir, Fake.parser());
+ }).isInstanceOf(ContextException.class)
+ .hasMessageContaining("Unable to read messages");
}
@Test
diff --git a/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java b/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java
index 345db7d8717..3bb3ee06e71 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java
@@ -36,12 +36,11 @@ import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.sonar.core.util.stream.MoreCollectors.index;
import static org.sonar.core.util.stream.MoreCollectors.join;
@@ -72,8 +71,6 @@ public class MoreCollectorsTest {
private static final List<MyObj> LIST = Arrays.asList(MY_OBJ_1_A, MY_OBJ_2_B, MY_OBJ_3_C);
private static final List<MyObj2> LIST2 = Arrays.asList(MY_OBJ2_1_A_X, MY_OBJ2_2_B, MY_OBJ2_3_C);
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void toList_builds_an_ImmutableList() {
@@ -197,126 +194,120 @@ public class MoreCollectorsTest {
public void uniqueIndex_fails_when_there_is_duplicate_keys() {
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream();
- expectedDuplicateKey1IAE();
-
- stream.collect(uniqueIndex(MyObj::getId));
+ assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Duplicate key 1");
}
@Test
public void uniqueIndex_with_expected_size_fails_when_there_is_duplicate_keys() {
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream();
- expectedDuplicateKey1IAE();
-
- stream.collect(uniqueIndex(MyObj::getId, 1));
+ assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId, 1)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Duplicate key 1");
}
@Test
public void uniqueIndex_with_valueFunction_fails_when_there_is_duplicate_keys() {
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream();
- expectedDuplicateKey1IAE();
-
- stream.collect(uniqueIndex(MyObj::getId, MyObj::getText));
+ assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId, MyObj::getText)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Duplicate key 1");
}
@Test
public void uniqueIndex_with_valueFunction_and_expected_size_fails_when_there_is_duplicate_keys() {
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream();
- expectedDuplicateKey1IAE();
-
- stream.collect(uniqueIndex(MyObj::getId, MyObj::getText, 10));
+ assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId, MyObj::getText, 10)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Duplicate key 1");
}
@Test
public void uniqueIndex_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- uniqueIndex(null);
+ assertThatThrownBy(() -> uniqueIndex(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void uniqueIndex_with_expected_size_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- uniqueIndex(null, 2);
+ assertThatThrownBy(() -> uniqueIndex(null, 2))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void uniqueIndex_with_valueFunction_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- uniqueIndex(null, MyObj::getText);
+ assertThatThrownBy(() -> uniqueIndex(null, MyObj::getText))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- uniqueIndex(null, MyObj::getText, 9);
+ assertThatThrownBy(() -> uniqueIndex(null, MyObj::getText, 9))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void uniqueIndex_with_valueFunction_fails_if_value_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Value function can't be null");
-
- uniqueIndex(MyObj::getId, null);
+ assertThatThrownBy(() -> uniqueIndex(MyObj::getId, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't be null");
}
@Test
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_value_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Value function can't be null");
-
- uniqueIndex(MyObj::getId, null, 9);
+ assertThatThrownBy(() -> uniqueIndex(MyObj::getId, null, 9))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't be null");
}
@Test
public void uniqueIndex_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void uniqueIndex_with_expected_size_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, 90));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, 90)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void uniqueIndex_with_valueFunction_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, MyObj::getText));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, MyObj::getText)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, MyObj::getText, 9));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, MyObj::getText, 9)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void uniqueIndex_with_valueFunction_fails_if_value_function_returns_null() {
- expectValueFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(MyObj::getId, s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(MyObj::getId, s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't return null");
}
@Test
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_value_function_returns_null() {
- expectValueFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(MyObj::getId, s -> null, 9));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(MyObj::getId, s -> null, 9)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't return null");
}
@Test
@@ -384,47 +375,44 @@ public class MoreCollectorsTest {
@Test
public void index_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- index(null);
+ assertThatThrownBy(() -> index(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void index_with_valueFunction_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- index(null, MyObj::getText);
+ assertThatThrownBy(() -> index(null, MyObj::getText))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void index_with_valueFunction_fails_if_value_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Value function can't be null");
-
- index(MyObj::getId, null);
+ assertThatThrownBy(() -> index(MyObj::getId, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't be null");
}
@Test
public void index_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(index(s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(index(s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void index_with_valueFunction_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(index(s -> null, MyObj::getText));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(index(s -> null, MyObj::getText)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void index_with_valueFunction_fails_if_value_function_returns_null() {
- expectValueFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(index(MyObj::getId, s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(index(MyObj::getId, s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't return null");
}
@Test
@@ -480,47 +468,44 @@ public class MoreCollectorsTest {
@Test
public void unorderedIndex_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- unorderedIndex(null);
+ assertThatThrownBy(() -> unorderedIndex(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void unorderedIndex_with_valueFunction_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- unorderedIndex(null, MyObj::getText);
+ assertThatThrownBy(() -> unorderedIndex(null, MyObj::getText))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void unorderedIndex_with_valueFunction_fails_if_value_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Value function can't be null");
-
- unorderedIndex(MyObj::getId, null);
+ assertThatThrownBy(() -> unorderedIndex(MyObj::getId, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't be null");
}
@Test
public void unorderedIndex_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(unorderedIndex(s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(unorderedIndex(s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void unorderedIndex_with_valueFunction_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(unorderedIndex(s -> null, MyObj::getText));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(unorderedIndex(s -> null, MyObj::getText)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void unorderedIndex_with_valueFunction_fails_if_value_function_returns_null() {
- expectValueFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT_LIST.stream().collect(unorderedIndex(MyObj::getId, s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(unorderedIndex(MyObj::getId, s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't return null");
}
@Test
@@ -577,32 +562,30 @@ public class MoreCollectorsTest {
@Test
public void unorderedFlattenIndex_with_valueFunction_fails_if_key_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't be null");
-
- unorderedFlattenIndex(null, MyObj2::getTexts);
+ assertThatThrownBy(() -> unorderedFlattenIndex(null, MyObj2::getTexts))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't be null");
}
@Test
public void unorderedFlattenIndex_with_valueFunction_fails_if_value_function_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Value function can't be null");
-
- unorderedFlattenIndex(MyObj2::getId, null);
+ assertThatThrownBy(() -> unorderedFlattenIndex(MyObj2::getId, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't be null");
}
@Test
public void unorderedFlattenIndex_with_valueFunction_fails_if_key_function_returns_null() {
- expectKeyFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT2_LIST.stream().collect(unorderedFlattenIndex(s -> null, MyObj2::getTexts));
+ assertThatThrownBy(() -> SINGLE_ELEMENT2_LIST.stream().collect(unorderedFlattenIndex(s -> null, MyObj2::getTexts)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key function can't return null");
}
@Test
public void unorderedFlattenIndex_with_valueFunction_fails_if_value_function_returns_null() {
- expectValueFunctionCantReturnNullNPE();
-
- SINGLE_ELEMENT2_LIST.stream().collect(unorderedFlattenIndex(MyObj2::getId, s -> null));
+ assertThatThrownBy(() -> SINGLE_ELEMENT2_LIST.stream().collect(unorderedFlattenIndex(MyObj2::getId, s -> null)))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Value function can't return null");
}
@Test
@@ -641,10 +624,9 @@ public class MoreCollectorsTest {
@Test
public void join_fails_with_NPE_if_joiner_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Joiner can't be null");
-
- join(null);
+ assertThatThrownBy(() -> join(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Joiner can't be null");
}
@Test
@@ -657,34 +639,17 @@ public class MoreCollectorsTest {
public void join_does_not_support_parallel_stream_and_fails_with_ISE() {
Stream<String> hugeStream = HUGE_LIST.parallelStream();
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Parallel processing is not supported");
-
- hugeStream.collect(join(Joiner.on(" ")));
+ assertThatThrownBy(() -> hugeStream.collect(join(Joiner.on(" "))))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Parallel processing is not supported");
}
@Test
public void join_supports_null_if_joiner_does() {
Stream<String> stream = Stream.of("1", null);
- expectedException.expect(NullPointerException.class);
-
- stream.collect(join(Joiner.on(",")));
- }
-
- private void expectedDuplicateKey1IAE() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Duplicate key 1");
- }
-
- private void expectKeyFunctionCantReturnNullNPE() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Key function can't return null");
- }
-
- private void expectValueFunctionCantReturnNullNPE() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Value function can't return null");
+ assertThatThrownBy(() -> stream.collect(join(Joiner.on(","))))
+ .isInstanceOf(NullPointerException.class);
}
private static final class MyObj {