aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
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-plugin-api
parent92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff)
downloadsonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz
sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java19
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java23
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java97
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java69
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java41
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/CeTaskBuilder_PostProjectAnalysisTaskTesterTest.java34
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ConditionBuilder_PostProjectAnalysisTaskTesterTest.java97
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java61
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ProjectBuilder_PostProjectAnalysisTaskTesterTest.java49
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/QualityGateBuilder_PostProjectAnalysisTaskTesterTest.java55
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java19
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java15
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/platform/NewUserHandlerTest.java11
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/AbstractLanguageTest.java12
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/CoreMetricsTest.java10
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java12
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTypeTest.java15
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java62
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/UserIdentityTest.java126
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java21
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoaderTest.java5
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java25
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java54
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/internal/DefaultNewRuleTest.java23
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java134
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java77
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/task/TaskDefinitionTest.java30
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/user/UserGroupValidationTest.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java49
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/PagingTest.java26
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java27
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/VersionTest.java13
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/ZipUtilsTest.java19
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java23
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java19
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java20
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/text/XmlWriterTest.java14
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java11
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/web/page/ContextTest.java12
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/web/page/PageTest.java49
42 files changed, 660 insertions, 944 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java
index a5baa329e31..ed0a3c8e0b2 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java
@@ -20,19 +20,16 @@
package org.sonar.api.batch.rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.NewActiveRule;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.SonarException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class CheckFactoryTest {
- @org.junit.Rule
- public ExpectedException thrown = ExpectedException.none();
-
ActiveRulesBuilder builder = new ActiveRulesBuilder();
@Test
@@ -81,9 +78,6 @@ public class CheckFactoryTest {
@Test
public void fail_if_missing_field() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty");
-
RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty");
NewActiveRule rule = new NewActiveRule.Builder()
.setRuleKey(ruleKey)
@@ -92,7 +86,10 @@ public class CheckFactoryTest {
builder.addRule(rule);
CheckFactory checkFactory = new CheckFactory(builder.build());
- checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class);
+
+ assertThatThrownBy(() -> checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty");
}
@Test
@@ -155,8 +152,6 @@ public class CheckFactoryTest {
@Test
public void fail_if_field_type_is_not_supported() {
- thrown.expect(SonarException.class);
-
RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithUnsupportedPropertyType");
NewActiveRule rule = new NewActiveRule.Builder()
.setRuleKey(ruleKey)
@@ -165,7 +160,9 @@ public class CheckFactoryTest {
builder.addRule(rule);
CheckFactory checkFactory = new CheckFactory(builder.build());
- checkFactory.create("squid").addAnnotatedChecks(CheckWithUnsupportedPropertyType.class);
+
+ assertThatThrownBy(() -> checkFactory.create("squid").addAnnotatedChecks(CheckWithUnsupportedPropertyType.class))
+ .isInstanceOf(SonarException.class);
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java
index 10f7272de9f..5ab77ef3597 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java
@@ -20,17 +20,13 @@
package org.sonar.api.batch.scm;
import java.nio.file.Paths;
-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 ScmProviderTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
private final ScmProvider provider = new ScmProvider() {
@Override
@@ -42,25 +38,26 @@ public class ScmProviderTest {
@Test
public void default_implementation_does_not_support_blame() {
assertThat(provider.supports(null)).isFalse();
- thrown.expect(UnsupportedOperationException.class);
- provider.blameCommand();
+
+ assertThatThrownBy(() -> provider.blameCommand())
+ .isInstanceOf(UnsupportedOperationException.class);
}
@Test
public void default_implementation_does_not_support_relativePathFromScmRoot() {
- thrown.expect(UnsupportedOperationException.class);
- provider.relativePathFromScmRoot(Paths.get("foo"));
+ assertThatThrownBy(() -> provider.relativePathFromScmRoot(Paths.get("foo")))
+ .isInstanceOf(UnsupportedOperationException.class);
}
@Test
public void default_implementation_does_not_support_revisionId() {
- thrown.expect(UnsupportedOperationException.class);
- provider.revisionId(Paths.get("foo"));
+ assertThatThrownBy(() -> provider.revisionId(Paths.get("foo")))
+ .isInstanceOf(UnsupportedOperationException.class);
}
@Test
public void default_implementation_does_not_support_ignore() {
- thrown.expect(UnsupportedOperationException.class);
- provider.ignoreCommand();
+ assertThatThrownBy(() -> provider.ignoreCommand())
+ .isInstanceOf(UnsupportedOperationException.class);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java
index 18127cc6b14..653a4bab15b 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java
@@ -19,18 +19,14 @@
*/
package org.sonar.api.ce.measure.test;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.ce.measure.Component;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class TestComponentTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void create_project() {
TestComponent component = new TestComponent("Project", Component.Type.PROJECT, null);
@@ -59,43 +55,39 @@ public class TestComponentTest {
@Test
public void fail_with_ISE_when_calling_get_file_attributes_on_not_file() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Only component of type FILE have a FileAttributes object");
-
TestComponent component = new TestComponent("Project", Component.Type.PROJECT, null);
- component.getFileAttributes();
+
+ assertThatThrownBy(() -> component.getFileAttributes())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Only component of type FILE have a FileAttributes object");
}
@Test
public void fail_with_IAE_when_trying_to_create_a_file_without_file_attributes() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("omponent of type FILE must have a FileAttributes object");
-
- new TestComponent("File", Component.Type.FILE, null);
+ assertThatThrownBy(() -> new TestComponent("File", Component.Type.FILE, null))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Component of type FILE must have a FileAttributes object");
}
@Test
public void fail_with_IAE_when_trying_to_create_not_a_file_with_file_attributes() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Only component of type FILE have a FileAttributes object");
-
- new TestComponent("Project", Component.Type.PROJECT, new TestComponent.FileAttributesImpl(null, true));
+ assertThatThrownBy(() -> new TestComponent("Project", Component.Type.PROJECT, new TestComponent.FileAttributesImpl(null, true)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Only component of type FILE have a FileAttributes object");
}
@Test
public void fail_with_NPE_when_creating_component_without_key() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Key cannot be null");
-
- new TestComponent(null, Component.Type.PROJECT, null);
+ assertThatThrownBy(() -> new TestComponent(null, Component.Type.PROJECT, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Key cannot be null");
}
@Test
public void fail_with_NPE_when_creating_component_without_type() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Type cannot be null");
-
- new TestComponent("Project", null, null);
+ assertThatThrownBy(() -> new TestComponent("Project", null, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Type cannot be null");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java
index d118978711e..3f304c4a084 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java
@@ -19,9 +19,7 @@
*/
package org.sonar.api.ce.measure.test;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.ce.measure.Issue;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
@@ -29,12 +27,10 @@ import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class TestIssueTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void create_issue() {
Issue issue = new TestIssue.Builder()
@@ -71,134 +67,123 @@ public class TestIssueTest {
@Test
public void fail_with_NPE_when_building_issue_without_key() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("key cannot be null");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setRuleKey(RuleKey.of("xoo", "S01"))
.setSeverity(Severity.BLOCKER)
.setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED)
.setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED)
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key cannot be null");
}
@Test
public void fail_with_NPE_when_creating_issue_with_null_key() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("key cannot be null");
-
- new TestIssue.Builder().setKey(null);
+ assertThatThrownBy(() -> new TestIssue.Builder().setKey(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key cannot be null");
}
@Test
public void fail_with_NPE_when_building_issue_without_rule_key() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("ruleKey cannot be null");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setKey("ABCD")
.setSeverity(Severity.BLOCKER)
.setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED)
.setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED)
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("ruleKey cannot be null");
}
@Test
public void fail_with_NPE_when_creating_issue_with_null_rule_key() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("ruleKey cannot be null");
-
- new TestIssue.Builder().setRuleKey(null);
+ assertThatThrownBy(() -> new TestIssue.Builder().setRuleKey(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("ruleKey cannot be null");
}
@Test
public void fail_with_IAE_when_building_issue_with_invalid_resolution() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("resolution 'unknown' is invalid");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setKey("ABCD")
.setRuleKey(RuleKey.of("xoo", "S01"))
.setSeverity(Severity.BLOCKER)
.setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED)
.setResolution("unknown")
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("resolution 'unknown' is invalid");
}
@Test
public void fail_with_NPE_when_building_issue_without_severity() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("severity cannot be null");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setKey("ABCD")
.setRuleKey(RuleKey.of("xoo", "S01"))
.setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED)
.setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED)
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("severity cannot be null");
}
@Test
public void fail_with_NPE_when_creating_issue_with_null_severity() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("severity cannot be null");
-
- new TestIssue.Builder().setSeverity(null);
+ assertThatThrownBy(() -> new TestIssue.Builder().setSeverity(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("severity cannot be null");
}
@Test
public void fail_with_IAE_when_building_issue_with_invalid_severity() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("severity 'unknown' is invalid");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setKey("ABCD")
.setRuleKey(RuleKey.of("xoo", "S01"))
.setSeverity("unknown")
.setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED)
.setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED)
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("severity 'unknown' is invalid");
}
@Test
public void fail_with_NPE_when_building_issue_without_status() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("status cannot be null");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setKey("ABCD")
.setRuleKey(RuleKey.of("xoo", "S01"))
.setSeverity(Severity.BLOCKER)
.setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED)
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
public void fail_with_NPE_when_creating_issue_with_null_status() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("status cannot be null");
-
- new TestIssue.Builder().setStatus(null);
+ assertThatThrownBy(() -> new TestIssue.Builder().setStatus(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
public void fail_with_IAE_when_building_issue_with_invalid_status() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("status 'unknown' is invalid");
-
- new TestIssue.Builder()
+ assertThatThrownBy(() -> new TestIssue.Builder()
.setKey("ABCD")
.setRuleKey(RuleKey.of("xoo", "S01"))
.setSeverity(Severity.BLOCKER)
.setStatus("unknown")
.setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED)
.setType(RuleType.BUG)
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("status 'unknown' is invalid");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java
index 44766b1cfe3..8afd124ff1f 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java
@@ -20,9 +20,7 @@
package org.sonar.api.ce.measure.test;
import java.util.Arrays;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.ce.measure.Component;
import org.sonar.api.ce.measure.Issue;
import org.sonar.api.ce.measure.Settings;
@@ -32,13 +30,11 @@ import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition;
public class TestMeasureComputerContextTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
static final String INPUT_METRIC = "INPUT_METRIC";
static final String OUTPUT_METRIC = "OUTPUT_METRIC";
@@ -100,10 +96,9 @@ public class TestMeasureComputerContextTest {
@Test
public void fail_with_IAE_when_trying_to_get_measure_on_unknown_metric() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Only metrics in [INPUT_METRIC] can be used to load measures");
-
- underTest.getMeasure("unknown");
+ assertThatThrownBy(() -> underTest.getMeasure("unknown"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Only metrics in [INPUT_METRIC] can be used to load measures");
}
@Test
@@ -136,10 +131,9 @@ public class TestMeasureComputerContextTest {
@Test
public void fail_with_IAE_when_trying_to_get_children_measures_on_unknown_metric() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Only metrics in [INPUT_METRIC] can be used to load measures");
-
- underTest.getChildrenMeasures("unknown");
+ assertThatThrownBy(() -> underTest.getChildrenMeasures("unknown"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Only metrics in [INPUT_METRIC] can be used to load measures");
}
@Test
@@ -172,27 +166,25 @@ public class TestMeasureComputerContextTest {
@Test
public void fail_with_IAE_when_trying_to_add_measure_on_unknown_metric() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Only metrics in [OUTPUT_METRIC] can be used to add measures. Metric 'unknown' is not allowed");
-
- underTest.addMeasure("unknown", 10);
+ assertThatThrownBy(() -> underTest.addMeasure("unknown", 10))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Only metrics in [OUTPUT_METRIC] can be used to add measures. Metric 'unknown' is not allowed.");
}
@Test
public void fail_with_IAE_when_trying_to_add_measure_on_input_metric() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Only metrics in [OUTPUT_METRIC] can be used to add measures. Metric 'INPUT_METRIC' is not allowed");
-
- underTest.addMeasure(INPUT_METRIC, 10);
+ assertThatThrownBy(() -> underTest.addMeasure(INPUT_METRIC, 10))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Only metrics in [OUTPUT_METRIC] can be used to add measures. Metric 'INPUT_METRIC' is not allowed.");
}
@Test
public void fail_with_UOE_when_trying_to_add_same_measures_twice() {
- thrown.expect(UnsupportedOperationException.class);
- thrown.expectMessage("A measure on metric 'OUTPUT_METRIC' already exists");
-
underTest.addMeasure(OUTPUT_METRIC, 10);
- underTest.addMeasure(OUTPUT_METRIC, 20);
+
+ assertThatThrownBy(() -> underTest.addMeasure(OUTPUT_METRIC, 20))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessage("A measure on metric 'OUTPUT_METRIC' already exists");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java
index d571812c232..552b9d49dfd 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java
@@ -19,20 +19,16 @@
*/
package org.sonar.api.ce.measure.test;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition;
import org.sonar.api.ce.measure.test.TestMeasureComputerDefinition.MeasureComputerDefinitionBuilderImpl;
import org.sonar.api.measures.CoreMetrics;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class TestMeasureComputerDefinitionTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void build_definition() {
MeasureComputerDefinition definition = new MeasureComputerDefinitionBuilderImpl()
@@ -56,77 +52,70 @@ public class TestMeasureComputerDefinitionTest {
@Test
public void fail_with_NPE_when_building_definition_with_null_input_metrics() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Input metrics cannot be null");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics((String[]) null)
.setOutputMetrics("OUTPUT_1", "OUTPUT_2")
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Input metrics cannot be null");
}
@Test
public void fail_with_NPE_when_building_definition_with_on_null_input_metric() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Null metric is not allowed");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics("INPUT_1", null)
.setOutputMetrics("OUTPUT_1", "OUTPUT_2")
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Null metric is not allowed");
}
@Test
public void fail_with_NPE_when_building_definition_with_null_output_metrics() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Output metrics cannot be null");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics("INPUT_1", "INPUT_2")
.setOutputMetrics((String[]) null)
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Output metrics cannot be null");
}
@Test
public void fail_with_NPE_when_building_definition_without_output_metrics() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Output metrics cannot be null");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics("INPUT_1", "INPUT_2")
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Output metrics cannot be null");
}
@Test
public void fail_with_NPE_when_building_definition_with_on_null_ouput_metric() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Null metric is not allowed");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics("INPUT_1", "INPUT_2")
.setOutputMetrics("OUTPUT_1", null)
- .build();
+ .build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Null metric is not allowed");
}
@Test
public void fail_with_IAE_when_building_definition_with_empty_output_metrics() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("At least one output metric must be defined");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics("INPUT_1", "INPUT_2")
.setOutputMetrics()
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("At least one output metric must be defined");
}
@Test
public void fail_with_IAE_when_building_definition_with_core_metrics_in_output_metrics() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Core metrics are not allowed");
-
- new MeasureComputerDefinitionBuilderImpl()
+ assertThatThrownBy(() -> new MeasureComputerDefinitionBuilderImpl()
.setInputMetrics("INPUT_1", "INPUT_2")
.setOutputMetrics(CoreMetrics.NCLOC_KEY)
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Core metrics are not allowed");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java
index b7da714131e..226bc732c8e 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java
@@ -19,17 +19,13 @@
*/
package org.sonar.api.ce.measure.test;
-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 TestMeasureTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void create_double_measure() {
assertThat(TestMeasure.createMeasure(10d).getDoubleValue()).isEqualTo(10d);
@@ -57,41 +53,36 @@ public class TestMeasureTest {
@Test
public void getDoubleValue_fails_with_ISE_when_not_a_double() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Not a double measure");
-
- TestMeasure.createMeasure(10).getDoubleValue();
+ assertThatThrownBy(() -> TestMeasure.createMeasure(10).getDoubleValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Not a double measure");
}
@Test
public void getIntValue_fails_with_ISE_when_not_an_int() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Not an integer measure");
-
- TestMeasure.createMeasure(10L).getIntValue();
+ assertThatThrownBy(() -> TestMeasure.createMeasure(10L).getIntValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Not an integer measure");
}
@Test
public void getLongValue_fails_with_ISE_when_not_a_long() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Not a long measure");
-
- TestMeasure.createMeasure(10).getLongValue();
+ assertThatThrownBy(() -> TestMeasure.createMeasure(10).getLongValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Not a long measure");
}
@Test
public void getStringValue_fails_with_ISE_when_not_a_string() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Not a string measure");
-
- TestMeasure.createMeasure(10).getStringValue();
+ assertThatThrownBy(() -> TestMeasure.createMeasure(10).getStringValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Not a string measure");
}
@Test
public void getBooleanValue_fails_with_ISE_when_not_a_boolean() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Not a boolean measure");
-
- TestMeasure.createMeasure(10).getBooleanValue();
+ assertThatThrownBy(() -> TestMeasure.createMeasure(10).getBooleanValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Not a boolean measure");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/CeTaskBuilder_PostProjectAnalysisTaskTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/CeTaskBuilder_PostProjectAnalysisTaskTesterTest.java
index 8c5939756d0..e2ab10017a1 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/CeTaskBuilder_PostProjectAnalysisTaskTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/CeTaskBuilder_PostProjectAnalysisTaskTesterTest.java
@@ -19,55 +19,47 @@
*/
package org.sonar.api.ce.posttask;
-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 CeTaskBuilder_PostProjectAnalysisTaskTesterTest {
private static final CeTask.Status SOME_STATUS = CeTask.Status.SUCCESS;
private static final String SOME_ID = "some id";
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private PostProjectAnalysisTaskTester.CeTaskBuilder underTest = PostProjectAnalysisTaskTester.newCeTaskBuilder();
@Test
public void setId_throws_NPE_if_id_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("id cannot be null");
-
- underTest.setId(null);
+ assertThatThrownBy(() -> underTest.setId(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("id cannot be null");
}
@Test
public void setStatus_throws_NPE_if_status_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("status cannot be null");
-
- underTest.setStatus(null);
+ assertThatThrownBy(() -> underTest.setStatus(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
public void build_throws_NPE_if_id_is_null() {
underTest.setStatus(SOME_STATUS);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("id cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("id cannot be null");
}
@Test
public void build_throws_NPE_if_status_is_null() {
underTest.setId(SOME_ID);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("status cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ConditionBuilder_PostProjectAnalysisTaskTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ConditionBuilder_PostProjectAnalysisTaskTesterTest.java
index 5b48077deb0..59d6b005252 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ConditionBuilder_PostProjectAnalysisTaskTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ConditionBuilder_PostProjectAnalysisTaskTesterTest.java
@@ -19,11 +19,10 @@
*/
package org.sonar.api.ce.posttask;
-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 ConditionBuilder_PostProjectAnalysisTaskTesterTest {
private static final String SOME_METRIC_KEY = "some metric key";
@@ -32,55 +31,47 @@ public class ConditionBuilder_PostProjectAnalysisTaskTesterTest {
private static final QualityGate.EvaluationStatus SOME_STATUS_BUT_NO_VALUE = QualityGate.EvaluationStatus.OK;
private static final String SOME_VALUE = "some value";
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private PostProjectAnalysisTaskTester.ConditionBuilder underTest = PostProjectAnalysisTaskTester.newConditionBuilder();
@Test
public void setMetricKey_throws_NPE_if_operator_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("metricKey cannot be null");
-
- underTest.setMetricKey(null);
+ assertThatThrownBy(() -> underTest.setMetricKey(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("metricKey cannot be null");
}
@Test
public void setOperator_throws_NPE_if_operator_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("operator cannot be null");
-
- underTest.setOperator(null);
+ assertThatThrownBy(() -> underTest.setOperator(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("operator cannot be null");
}
@Test
public void buildNoValue_throws_NPE_if_metricKey_is_null() {
underTest.setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("metricKey cannot be null");
-
- underTest.buildNoValue();
+ assertThatThrownBy(() -> underTest.buildNoValue())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("metricKey cannot be null");
}
@Test
public void buildNoValue_throws_NPE_if_operator_is_null() {
underTest.setMetricKey(SOME_METRIC_KEY).setErrorThreshold(SOME_ERROR_THRESHOLD);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("operator cannot be null");
-
- underTest.buildNoValue();
+ assertThatThrownBy(() -> underTest.buildNoValue())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("operator cannot be null");
}
@Test
public void buildNoValue_throws_NPE_if_errorThreshold_is_null() {
underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("errorThreshold cannot be null");
-
- underTest.buildNoValue();
+ assertThatThrownBy(() -> underTest.buildNoValue())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("errorThreshold cannot be null");
}
@Test
@@ -95,10 +86,9 @@ public class ConditionBuilder_PostProjectAnalysisTaskTesterTest {
initValidBuilder();
QualityGate.Condition condition = underTest.buildNoValue();
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("There is no value when status is NO_VALUE");
-
- condition.getValue();
+ assertThatThrownBy(() -> condition.getValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("There is no value when status is NO_VALUE");
}
@Test
@@ -133,60 +123,54 @@ public class ConditionBuilder_PostProjectAnalysisTaskTesterTest {
public void build_throws_NPE_if_status_is_null() {
initValidBuilder();
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("status cannot be null");
-
- underTest.build(null, SOME_VALUE);
+ assertThatThrownBy(() -> underTest.build(null, SOME_VALUE))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
public void build_throws_IAE_if_status_is_NO_VALUE() {
initValidBuilder();
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("status cannot be NO_VALUE, use method buildNoValue() instead");
-
- underTest.build(QualityGate.EvaluationStatus.NO_VALUE, SOME_VALUE);
+ assertThatThrownBy(() -> underTest.build(QualityGate.EvaluationStatus.NO_VALUE, SOME_VALUE))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("status cannot be NO_VALUE, use method buildNoValue() instead");
}
@Test
public void build_throws_NPE_if_value_is_null() {
initValidBuilder();
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("value cannot be null, use method buildNoValue() instead");
-
- underTest.build(SOME_STATUS_BUT_NO_VALUE, null);
+ assertThatThrownBy(() -> underTest.build(SOME_STATUS_BUT_NO_VALUE, null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("value cannot be null, use method buildNoValue() instead");
}
@Test
public void build_throws_NPE_if_metricKey_is_null() {
underTest.setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("metricKey cannot be null");
-
- underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
+ assertThatThrownBy(() -> underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("metricKey cannot be null");
}
@Test
public void build_throws_NPE_if_operator_is_null() {
underTest.setMetricKey(SOME_METRIC_KEY).setErrorThreshold(SOME_ERROR_THRESHOLD);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("operator cannot be null");
-
- underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
+ assertThatThrownBy(() -> underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("operator cannot be null");
}
@Test
public void build_throws_NPE_if_errorThreshold_is_null() {
underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("errorThreshold cannot be null");
-
- underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
+ assertThatThrownBy(() -> underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("errorThreshold cannot be null");
}
@Test
@@ -218,10 +202,9 @@ public class ConditionBuilder_PostProjectAnalysisTaskTesterTest {
initValidBuilder();
QualityGate.Condition condition = underTest.buildNoValue();
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("There is no value when status is NO_VALUE");
-
- condition.getValue();
+ assertThatThrownBy(() -> condition.getValue())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("There is no value when status is NO_VALUE");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java
index 949dcb63d3d..528caa25969 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java
@@ -24,11 +24,10 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang.RandomStringUtils;
-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.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
@@ -36,8 +35,6 @@ import static org.mockito.Mockito.when;
import static org.sonar.api.ce.posttask.Branch.Type.BRANCH;
public class PostProjectAnalysisTaskTesterTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private CeTask ceTask = mock(CeTask.class);
private Project project = mock(Project.class);
@@ -50,34 +47,30 @@ public class PostProjectAnalysisTaskTesterTest {
@Test
public void of_throws_NPE_if_PostProjectAnalysisTask_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("PostProjectAnalysisTask instance cannot be null");
-
- PostProjectAnalysisTaskTester.of(null);
+ assertThatThrownBy(() -> PostProjectAnalysisTaskTester.of(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("PostProjectAnalysisTask instance cannot be null");
}
@Test
public void withCeTask_throws_NPE_if_ceTask_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("ceTask cannot be null");
-
- underTest.withCeTask(null);
+ assertThatThrownBy(() -> underTest.withCeTask(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("ceTask cannot be null");
}
@Test
public void withProject_throws_NPE_if_project_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("project cannot be null");
-
- underTest.withProject(null);
+ assertThatThrownBy(() -> underTest.withProject(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("project cannot be null");
}
@Test
public void at_throws_NPE_if_date_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("date cannot be null");
-
- underTest.at(null);
+ assertThatThrownBy(() -> underTest.at(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("date cannot be null");
}
@Test
@@ -89,20 +82,18 @@ public class PostProjectAnalysisTaskTesterTest {
public void execute_throws_NPE_if_ceTask_is_null() {
underTest.withProject(project).at(someDate);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("ceTask cannot be null");
-
- underTest.execute();
+ assertThatThrownBy(() -> underTest.execute())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("ceTask cannot be null");
}
@Test
public void execute_throws_NPE_if_project_is_null() {
underTest.withCeTask(ceTask).at(someDate);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("project cannot be null");
-
- underTest.execute();
+ assertThatThrownBy(() -> underTest.execute())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("project cannot be null");
}
@Test
@@ -137,18 +128,16 @@ public class PostProjectAnalysisTaskTesterTest {
public void execute_throws_NPE_if_date_is_null() {
underTest.withCeTask(ceTask).withProject(project);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("date cannot be null");
-
- underTest.execute();
+ assertThatThrownBy(() -> underTest.execute())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("date cannot be null");
}
@Test
public void getLogStatistics_throws_ISE_if_called_before_execute() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("execute must be called first");
-
- underTest.getLogStatistics();
+ assertThatThrownBy(() -> underTest.getLogStatistics())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("execute must be called first");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ProjectBuilder_PostProjectAnalysisTaskTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ProjectBuilder_PostProjectAnalysisTaskTesterTest.java
index d6012f227f7..e16b866c119 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ProjectBuilder_PostProjectAnalysisTaskTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/ProjectBuilder_PostProjectAnalysisTaskTesterTest.java
@@ -19,75 +19,64 @@
*/
package org.sonar.api.ce.posttask;
-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 ProjectBuilder_PostProjectAnalysisTaskTesterTest {
private static final String SOME_NAME = "some name";
private static final String SOME_KEY = "some key";
private static final String SOME_UUID = "some uuid";
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private PostProjectAnalysisTaskTester.ProjectBuilder underTest = PostProjectAnalysisTaskTester.newProjectBuilder();
@Test
public void setKey_throws_NPE_if_key_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("key cannot be null");
-
- underTest.setKey(null);
+ assertThatThrownBy(() -> underTest.setKey(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key cannot be null");
}
@Test
public void setName_throws_NPE_if_name_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("name cannot be null");
-
- underTest.setName(null);
+ assertThatThrownBy(() -> underTest.setName(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("name cannot be null");
}
@Test
public void setUuid_throws_NPE_if_uuid_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("uuid cannot be null");
-
- underTest.setUuid(null);
+ assertThatThrownBy(() -> underTest.setUuid(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("uuid cannot be null");
}
@Test
public void build_throws_NPE_if_key_is_null() {
underTest.setUuid(SOME_UUID).setName(SOME_NAME);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("key cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key cannot be null");
}
@Test
public void build_throws_NPE_if_name_is_null() {
underTest.setUuid(SOME_UUID).setKey(SOME_KEY);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("name cannot be null");
-
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("name cannot be null");
}
@Test
public void build_throws_NPE_if_uuid_is_null() {
underTest.setKey(SOME_KEY).setName(SOME_NAME);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("uuid cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("uuid cannot be null");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/QualityGateBuilder_PostProjectAnalysisTaskTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/QualityGateBuilder_PostProjectAnalysisTaskTesterTest.java
index 85bcc7667f3..da3fb12f2e2 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/QualityGateBuilder_PostProjectAnalysisTaskTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/QualityGateBuilder_PostProjectAnalysisTaskTesterTest.java
@@ -19,11 +19,10 @@
*/
package org.sonar.api.ce.posttask;
-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;
@@ -32,43 +31,36 @@ public class QualityGateBuilder_PostProjectAnalysisTaskTesterTest {
private static final QualityGate.Status SOME_STATUS = QualityGate.Status.ERROR;
private static final String SOME_ID = "some id";
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private QualityGate.Condition condition1 = mock(QualityGate.Condition.class);
private QualityGate.Condition condition2 = mock(QualityGate.Condition.class);
private PostProjectAnalysisTaskTester.QualityGateBuilder underTest = PostProjectAnalysisTaskTester.newQualityGateBuilder();
@Test
public void setId_throws_NPE_if_id_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("id cannot be null");
-
- underTest.setId(null);
+ assertThatThrownBy(() -> underTest.setId(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("id cannot be null");
}
@Test
public void setStatus_throws_NPE_if_status_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("status cannot be null");
-
- underTest.setStatus(null);
+ assertThatThrownBy(() -> underTest.setStatus(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
public void setName_throws_NPE_if_name_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("name cannot be null");
-
- underTest.setName(null);
+ assertThatThrownBy(() -> underTest.setName(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("name cannot be null");
}
@Test
public void addCondition_throws_NPE_if_condition_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("condition cannot be null");
-
- underTest.add(null);
+ assertThatThrownBy(() -> underTest.add(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("condition cannot be null");
}
@Test
@@ -91,30 +83,27 @@ public class QualityGateBuilder_PostProjectAnalysisTaskTesterTest {
public void build_throws_NPE_if_id_is_null() {
underTest.setStatus(SOME_STATUS).setName(SOME_NAME);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("id cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("id cannot be null");
}
@Test
public void build_throws_NPE_if_status_is_null() {
underTest.setId(SOME_ID).setName(SOME_NAME);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("status cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("status cannot be null");
}
@Test
public void build_throws_NPE_if_name_is_null() {
underTest.setId(SOME_ID).setStatus(SOME_STATUS);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("name cannot be null");
-
- underTest.build();
+ assertThatThrownBy(() -> underTest.build())
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("name cannot be null");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java
index 1488897faf1..82d3b378255 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java
@@ -25,9 +25,7 @@ import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import org.apache.commons.lang.RandomStringUtils;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.api.resources.Qualifiers;
@@ -35,12 +33,11 @@ import org.sonar.api.utils.System2;
import static java.util.Collections.singletonList;
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 PropertyDefinitionsTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void should_build_with_predefined_list_of_definitions() {
@@ -182,20 +179,18 @@ public class PropertyDefinitionsTest {
public void validKey_throws_NPE_if_key_is_null() {
PropertyDefinitions underTest = new PropertyDefinitions(System2.INSTANCE);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("key can't be null");
-
- underTest.validKey(null);
+ assertThatThrownBy(() -> underTest.validKey(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key can't be null");
}
@Test
public void get_throws_NPE_if_key_is_null() {
PropertyDefinitions underTest = new PropertyDefinitions(System2.INSTANCE);
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("key can't be null");
-
- underTest.get(null);
+ assertThatThrownBy(() -> underTest.get(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key can't be null");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java
index d4bb10443c3..b321ec2add9 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java
@@ -19,17 +19,13 @@
*/
package org.sonar.api.measures;
-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 MetricTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void shouldCreateMetric() {
Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.INT)
@@ -82,12 +78,11 @@ public class MetricTest {
@Test
public void fail_if_decimal_scale_is_greater_than_max_supported_value() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Scale of decimal metric [foo] must be less than or equal 5: 6");
-
- new Metric.Builder("foo", "Foo", Metric.ValueType.FLOAT)
+ assertThatThrownBy(() -> new Metric.Builder("foo", "Foo", Metric.ValueType.FLOAT)
.setDecimalScale(6)
- .create();
+ .create())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Scale of decimal metric [foo] must be less than or equal 5: 6");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/platform/NewUserHandlerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/platform/NewUserHandlerTest.java
index 1ae7a0e154a..8ff7d88bbff 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/platform/NewUserHandlerTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/platform/NewUserHandlerTest.java
@@ -19,17 +19,13 @@
*/
package org.sonar.api.platform;
-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 NewUserHandlerTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void build_context() {
NewUserHandler.Context context = NewUserHandler.Context.builder().setLogin("marius").setName("Marius").setEmail("marius@lesbronzes.fr").build();
@@ -41,8 +37,7 @@ public class NewUserHandlerTest {
@Test
public void login_is_mandatory() {
- thrown.expect(NullPointerException.class);
-
- NewUserHandler.Context.builder().setName("Marius").build();
+ assertThatThrownBy(() -> NewUserHandler.Context.builder().setName("Marius").build())
+ .isInstanceOf(NullPointerException.class);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/AbstractLanguageTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/AbstractLanguageTest.java
index 0fd0d8f0b55..5f5a58cc2f8 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/AbstractLanguageTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/AbstractLanguageTest.java
@@ -19,16 +19,13 @@
*/
package org.sonar.api.resources;
-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 AbstractLanguageTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Test
public void test_equals_and_hashcode() {
@@ -63,10 +60,9 @@ public class AbstractLanguageTest {
@Test
public void should_not_define_language_with_too_long_key() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("The following language key exceeds 20 characters: 'aKeyWhichIsVeryVeryVeryVeryVeryLong'");
-
- new TooLongKeyLanguage();
+ assertThatThrownBy(() -> new TooLongKeyLanguage())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The following language key exceeds 20 characters: 'aKeyWhichIsVeryVeryVeryVeryVeryLong'");
}
static class TooLongKeyLanguage extends AbstractLanguage {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/CoreMetricsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/CoreMetricsTest.java
index 319ffb959f5..5b9505f4f25 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/CoreMetricsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/CoreMetricsTest.java
@@ -21,12 +21,11 @@ package org.sonar.api.resources;
import java.util.List;
import java.util.NoSuchElementException;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.measures.Metric;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.measures.CoreMetrics.DIRECTORIES;
import static org.sonar.api.measures.CoreMetrics.NCLOC;
import static org.sonar.api.measures.CoreMetrics.getMetric;
@@ -34,9 +33,6 @@ import static org.sonar.api.measures.CoreMetrics.getMetrics;
public class CoreMetricsTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void read_metrics_from_class_reflection() {
List<Metric> metrics = getMetrics();
@@ -52,7 +48,7 @@ public class CoreMetricsTest {
@Test
public void fail_get_unknown_metric_by_key() {
- expectedException.expect(NoSuchElementException.class);
- getMetric("unknown");
+ assertThatThrownBy(() -> getMetric("unknown"))
+ .isInstanceOf(NoSuchElementException.class);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
index 793f5e04095..b836fe313c3 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
@@ -22,18 +22,15 @@ package org.sonar.api.rules;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.PropertyType;
import org.sonar.api.utils.SonarException;
import org.sonar.check.Priority;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class AnnotationRuleParserTest {
- @org.junit.Rule
- public final ExpectedException exception = ExpectedException.none();
-
@Test
public void rule_with_property() {
List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class);
@@ -75,10 +72,9 @@ public class AnnotationRuleParserTest {
@Test
public void should_reject_invalid_property_types() {
- exception.expect(SonarException.class);
- exception.expectMessage("Invalid property type [INVALID]");
-
- parseAnnotatedClass(RuleWithInvalidPropertyType.class);
+ assertThatThrownBy(() -> parseAnnotatedClass(RuleWithInvalidPropertyType.class))
+ .isInstanceOf(SonarException.class)
+ .hasMessage("Invalid property type [INVALID]");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTypeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTypeTest.java
index a23943d1f7e..0bfe818de8f 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTypeTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTypeTest.java
@@ -19,16 +19,13 @@
*/
package org.sonar.api.rules;
-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 RuleTypeTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void test_valueOf_db_constant() {
@@ -38,9 +35,9 @@ public class RuleTypeTest {
@Test
public void valueOf_throws_ISE_if_unsupported_db_constant() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Unsupported type value : 5");
- RuleType.valueOf(5);
+ assertThatThrownBy(() -> RuleType.valueOf(5))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Unsupported type value : 5");
}
@Test
@@ -50,7 +47,7 @@ public class RuleTypeTest {
@Test
public void ALL_NAMES_is_immutable() {
- expectedException.expect(UnsupportedOperationException.class);
- RuleType.names().add("foo");
+ assertThatThrownBy(() -> RuleType.names().add("foo"))
+ .isInstanceOf(UnsupportedOperationException.class);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java
index 8f40710f1fe..9bb08a06752 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java
@@ -19,17 +19,13 @@
*/
package org.sonar.api.server.authentication;
-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 DisplayTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void create_display() {
Display display = Display.builder()
@@ -64,67 +60,61 @@ public class DisplayTest {
@Test
public void fail_when_icon_path_is_null() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Icon path must not be blank");
-
- Display.builder()
+ assertThatThrownBy(() -> Display.builder()
.setIconPath(null)
.setBackgroundColor("#123456")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Icon path must not be blank");
}
@Test
public void fail_when_icon_path_is_blank() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Icon path must not be blank");
-
- Display.builder()
+ assertThatThrownBy(() -> Display.builder()
.setIconPath("")
.setBackgroundColor("#123456")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Icon path must not be blank");
}
@Test
public void fail_when_background_color_is_null() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Background color must not be blank");
-
- Display.builder()
+ assertThatThrownBy(() -> Display.builder()
.setIconPath("/static/authgithub/github.svg")
.setBackgroundColor(null)
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Background color must not be blank");
}
@Test
public void fail_when_background_color_is_blank() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Background color must not be blank");
-
- Display.builder()
+ assertThatThrownBy(() -> Display.builder()
.setIconPath("/static/authgithub/github.svg")
.setBackgroundColor("")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Background color must not be blank");
}
@Test
public void fail_when_background_color_has_wrong_size() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Background color must begin with a sharp followed by 6 characters");
-
- Display.builder()
+ assertThatThrownBy(() -> Display.builder()
.setIconPath("/static/authgithub/github.svg")
- .setBackgroundColor("#1234")
- .build();
+ .setBackgroundColor("1234")
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Background color must begin with a sharp followed by 6 characters");
}
@Test
public void fail_when_background_color_doesnt_begin_with_sharp() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Background color must begin with a sharp followed by 6 characters");
-
- Display.builder()
+ assertThatThrownBy(() -> Display.builder()
.setIconPath("/static/authgithub/github.svg")
.setBackgroundColor("*123456")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Background color must begin with a sharp followed by 6 characters");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/UserIdentityTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/UserIdentityTest.java
index cf7be52ec41..8db67613cdf 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/UserIdentityTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/UserIdentityTest.java
@@ -20,18 +20,14 @@
package org.sonar.api.server.authentication;
import com.google.common.base.Strings;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class UserIdentityTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void create_user() {
UserIdentity underTest = UserIdentity.builder()
@@ -66,88 +62,88 @@ public class UserIdentityTest {
@Test
public void fail_when_id_is_too_long() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("ID is too big (255 characters max)");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderId(Strings.repeat("1", 256))
.setProviderLogin("john")
.setName("John")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("ID is too big (255 characters max)");
}
@Test
public void fail_when_provider_login_is_null() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Provider login must not be blank");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setName("John")
.setEmail("john@email.com")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Provider login must not be blank");
}
@Test
public void fail_when_provider_login_is_empty() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Provider login must not be blank");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("")
.setName("John")
.setEmail("john@email.com")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Provider login must not be blank");
}
@Test
public void fail_when_provider_login_is_too_long() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Provider login size is incorrect (maximum 255 characters)");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin(Strings.repeat("1", 256))
.setName("John")
.setEmail("john@email.com")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Provider login size is incorrect (maximum 255 characters)");
}
@Test
public void fail_when_name_is_null() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User name must not be blank");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setEmail("john@email.com")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("User name must not be blank");
}
@Test
public void fail_when_name_is_empty() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User name must not be blank");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName("")
.setEmail("john@email.com")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("User name must not be blank");
}
@Test
public void fail_when_name_is_loo_long() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User name size is too big (200 characters max)");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName(Strings.repeat("1", 201))
.setEmail("john@email.com")
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("User name size is too big (200 characters max)");
}
@Test
public void fail_when_email_is_loo_long() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User email size is too big (100 characters max)");
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName("John")
.setEmail(Strings.repeat("1", 101))
- .build();
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("User email size is too big (100 characters max)");
}
@Test
@@ -165,74 +161,66 @@ public class UserIdentityTest {
@Test
public void fail_when_groups_are_null() {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Groups cannot be null, please don't use this method if groups should not be synchronized.");
-
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
- .setName("John")
.setEmail("john@email.com")
- .setGroups(null);
+ .setGroups(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("Groups cannot be null, please don't use this method if groups should not be synchronized.");
}
@Test
public void fail_when_groups_contain_empty_group_name() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be empty");
-
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
- .setName("John")
.setEmail("john@email.com")
- .setGroups(newHashSet(""));
+ .setGroups(newHashSet("")))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be empty");
}
@Test
public void fail_when_groups_contain_only_blank_space() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be empty");
-
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName("John")
.setEmail("john@email.com")
- .setGroups(newHashSet(" "));
+ .setGroups(newHashSet(" ")))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be empty");
}
@Test
public void fail_when_groups_contain_null_group_name() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be empty");
-
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName("John")
.setEmail("john@email.com")
- .setGroups(newHashSet((String)null));
+ .setGroups(newHashSet((String)null)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be empty");
}
@Test
public void fail_when_groups_contain_anyone() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Anyone group cannot be used");
-
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName("John")
.setEmail("john@email.com")
- .setGroups(newHashSet("Anyone"));
+ .setGroups(newHashSet("Anyone")))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Anyone group cannot be used");
}
@Test
public void fail_when_groups_contain_too_long_group_name() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be longer than 255 characters");
-
- UserIdentity.builder()
+ assertThatThrownBy(() -> UserIdentity.builder()
.setProviderLogin("john")
.setName("John")
.setEmail("john@email.com")
- .setGroups(newHashSet(Strings.repeat("group", 300)));
+ .setGroups(newHashSet(Strings.repeat("group", 300))))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be longer than 255 characters");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java
index 7e38dcedb84..947980d6d8f 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java
@@ -21,22 +21,18 @@ package org.sonar.api.server.profile;
import java.util.Map;
import java.util.function.Consumer;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.jupiter.api.Assertions;
-import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInActiveRule;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.groups.Tuple.tuple;
public class BuiltInQualityProfilesDefinitionTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Test
public void coverage() {
@@ -110,12 +106,12 @@ public class BuiltInQualityProfilesDefinitionTest {
@Test
public void duplicateProfile() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("There is already a quality profile with name 'Foo' for language 'xoo'");
- define(c -> {
+ assertThatThrownBy(() -> define(c -> {
c.createBuiltInQualityProfile("Foo", "xoo").done();
c.createBuiltInQualityProfile("Foo", "xoo").done();
- });
+ }))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("There is already a quality profile with name 'Foo' for language 'xoo'");
}
@Test
@@ -151,10 +147,9 @@ public class BuiltInQualityProfilesDefinitionTest {
NewBuiltInQualityProfile profile = c.createBuiltInQualityProfile("Foo", "xoo");
profile.activateRule("repo", "rule");
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("The rule 'repo:rule' is already activated");
-
- profile.activateRule("repo", "rule");
+ assertThatThrownBy(() -> profile.activateRule("repo", "rule"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The rule 'repo:rule' is already activated");
});
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoaderTest.java
index 74a801f6f51..c7b019e83b5 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoaderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoaderTest.java
@@ -20,19 +20,16 @@
package org.sonar.api.server.rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
+import org.sonar.api.impl.server.RulesDefinitionContext;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RulesDefinition.NewRule;
import org.sonar.check.Priority;
-import org.sonar.api.impl.server.RulesDefinitionContext;
import static org.assertj.core.api.Assertions.assertThat;
public class RulesDefinitionAnnotationLoaderTest {
- @org.junit.Rule
- public final ExpectedException thrown = ExpectedException.none();
RulesDefinitionAnnotationLoader annotationLoader = new RulesDefinitionAnnotationLoader();
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
index a965ce51e76..287c8e1a0a2 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
@@ -32,7 +32,6 @@ import java.util.stream.Collectors;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.sonar.api.impl.server.RulesDefinitionContext;
import org.sonar.api.rule.RuleKey;
@@ -44,6 +43,7 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.utils.log.LogTester;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.fail;
@RunWith(DataProviderRunner.class)
@@ -54,9 +54,6 @@ public class RulesDefinitionTest {
@Rule
public LogTester logTester = new LogTester();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void define_repositories() {
assertThat(context.repositories()).isEmpty();
@@ -268,10 +265,9 @@ public class RulesDefinitionTest {
RulesDefinition.NewRepository newRepository = context.createRepository("foo", "bar");
RulesDefinition.NewRule newRule = newRepository.createRule("doh");
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Repository must be set");
-
- newRule.addDeprecatedRuleKey(nullOrEmpty, "oldKey");
+ assertThatThrownBy(() -> newRule.addDeprecatedRuleKey(nullOrEmpty, "oldKey"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Repository must be set");
}
@Test
@@ -280,10 +276,9 @@ public class RulesDefinitionTest {
RulesDefinition.NewRepository newRepository = context.createRepository("foo", "bar");
RulesDefinition.NewRule newRule = newRepository.createRule("doh");
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Rule must be set");
-
- newRule.addDeprecatedRuleKey("oldRepo", nullOrEmpty);
+ assertThatThrownBy(() -> newRule.addDeprecatedRuleKey("oldRepo", nullOrEmpty))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Rule must be set");
}
@DataProvider
@@ -431,11 +426,11 @@ public class RulesDefinitionTest {
@Test
public void fail_if_duplicated_rule_keys_in_the_same_repository() {
- expectedException.expect(IllegalArgumentException.class);
-
RulesDefinition.NewRepository findbugs = context.createRepository("findbugs", "java");
findbugs.createRule("NPE");
- findbugs.createRule("NPE");
+
+ assertThatThrownBy(() -> findbugs.createRule("NPE"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java
index 47a80d25b28..b4ab0191331 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java
@@ -24,21 +24,17 @@ import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
+import org.sonar.api.impl.server.RulesDefinitionContext;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.debt.DebtRemediationFunction;
-import org.sonar.api.impl.server.RulesDefinitionContext;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.api.utils.ExceptionCauseMatcher.hasType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class RulesDefinitionXmlLoaderTest {
- @org.junit.Rule
- public final ExpectedException expectedException = ExpectedException.none();
-
RulesDefinitionXmlLoader underTest = new RulesDefinitionXmlLoader();
@Test
@@ -76,29 +72,31 @@ public class RulesDefinitionXmlLoaderTest {
@Test
public void fail_if_missing_rule_key() {
- expectedException.expect(IllegalStateException.class);
- load(IOUtils.toInputStream("<rules><rule><name>Foo</name></rule></rules>"), StandardCharsets.UTF_8.name());
+ assertThatThrownBy(() -> load(IOUtils.toInputStream("<rules><rule><name>Foo</name></rule></rules>"), StandardCharsets.UTF_8.name()))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_if_missing_property_key() {
- expectedException.expect(IllegalStateException.class);
- load(IOUtils.toInputStream("<rules><rule><key>foo</key><name>Foo</name><param></param></rule></rules>"), StandardCharsets.UTF_8.name());
+ assertThatThrownBy(() -> load(IOUtils.toInputStream("<rules><rule><key>foo</key><name>Foo</name><param></param></rule></rules>"),
+ StandardCharsets.UTF_8.name()))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_on_invalid_rule_parameter_type() {
- expectedException.expect(IllegalStateException.class);
- load(IOUtils.toInputStream("<rules><rule><key>foo</key><name>Foo</name><param><key>key</key><type>INVALID</type></param></rule></rules>"), StandardCharsets.UTF_8.name());
+ assertThatThrownBy(() -> load(IOUtils.toInputStream("<rules><rule><key>foo</key><name>Foo</name><param><key>key</key><type>INVALID</type></param></rule></rules>"),
+ StandardCharsets.UTF_8.name()))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_if_invalid_xml() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("XML is not valid");
-
InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/invalid.xml");
- load(input, StandardCharsets.UTF_8.name());
+
+ assertThatThrownBy(() -> load(input, StandardCharsets.UTF_8.name()))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("XML is not valid");
}
@Test
@@ -213,12 +211,7 @@ public class RulesDefinitionXmlLoaderTest {
@Test
public void fail_if_invalid_remediation_function() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Fail to load the rule with key [squid:1]");
- expectedException.expectCause(hasType(IllegalArgumentException.class)
- .andMessage("No enum constant org.sonar.api.server.debt.DebtRemediationFunction.Type.UNKNOWN"));
-
- load("" +
+ assertThatThrownBy(() -> load("" +
"<rules>" +
" <rule>" +
" <key>1</key>" +
@@ -226,7 +219,11 @@ public class RulesDefinitionXmlLoaderTest {
" <description>Desc</description>" +
" <remediationFunction>UNKNOWN</remediationFunction>" +
" </rule>" +
- "</rules>");
+ "</rules>"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Fail to load the rule with key [squid:1]")
+ .hasCauseInstanceOf(IllegalArgumentException.class)
+ .hasRootCauseMessage("No enum constant org.sonar.api.server.debt.DebtRemediationFunction.Type.UNKNOWN");
}
@Test
@@ -257,12 +254,11 @@ public class RulesDefinitionXmlLoaderTest {
" </rule>" +
"</rules>";
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Fail to load the rule with key [squid:1]");
- expectedException.expectCause(hasType(IllegalArgumentException.class)
- .andMessage("No enum constant org.sonar.api.server.rule.RulesDefinitionXmlLoader.DescriptionFormat.UNKNOWN"));
-
- load(xml).rule("1");
+ assertThatThrownBy(() -> load(xml).rule("1"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Fail to load the rule with key [squid:1]")
+ .hasCauseInstanceOf(IllegalArgumentException.class)
+ .hasRootCauseMessage("No enum constant org.sonar.api.server.rule.RulesDefinitionXmlLoader.DescriptionFormat.UNKNOWN");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/internal/DefaultNewRuleTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/internal/DefaultNewRuleTest.java
index a8eb9d21bae..adb9ccede87 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/internal/DefaultNewRuleTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/internal/DefaultNewRuleTest.java
@@ -19,9 +19,7 @@
*/
package org.sonar.api.server.rule.internal;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleScope;
import org.sonar.api.rule.RuleStatus;
@@ -30,11 +28,10 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
public class DefaultNewRuleTest {
- @Rule
- public ExpectedException exception = ExpectedException.none();
private DefaultNewRule rule = new DefaultNewRule("plugin", "repo", "key");
@@ -97,8 +94,9 @@ public class DefaultNewRuleTest {
@Test
public void validate_fails() {
rule.setHtmlDescription("html");
- exception.expect(IllegalStateException.class);
- rule.validate();
+
+ assertThatThrownBy(() -> rule.validate())
+ .isInstanceOf(IllegalStateException.class);
}
@Test
@@ -115,20 +113,21 @@ public class DefaultNewRuleTest {
}
@Test
public void fail_if_severity_is_invalid() {
- exception.expect(IllegalArgumentException.class);
- rule.setSeverity("invalid");
+ assertThatThrownBy(() -> rule.setSeverity("invalid"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void fail_setting_markdown_if_html_is_set() {
- exception.expect(IllegalStateException.class);
rule.setHtmlDescription("html");
- rule.setMarkdownDescription("markdown");
+
+ assertThatThrownBy(() -> rule.setMarkdownDescription("markdown"))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
public void fail_if_set_status_to_removed() {
- exception.expect(IllegalArgumentException.class);
- rule.setStatus(RuleStatus.REMOVED);
+ assertThatThrownBy(() -> rule.setStatus(RuleStatus.REMOVED))
+ .isInstanceOf(IllegalArgumentException.class);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
index a484ebdd241..b5409eb8173 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
@@ -36,13 +36,11 @@ import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
-import org.sonar.api.rule.RuleStatus;
import org.sonar.api.impl.ws.PartImpl;
import org.sonar.api.impl.ws.ValidatingRequest;
+import org.sonar.api.rule.RuleStatus;
import org.sonar.api.utils.DateUtils;
import static com.google.common.base.Strings.repeat;
@@ -52,16 +50,12 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import static org.sonar.api.utils.DateUtils.parseDate;
import static org.sonar.api.utils.DateUtils.parseDateTime;
@RunWith(DataProviderRunner.class)
public class RequestTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private FakeRequest underTest = new FakeRequest();
@Before
@@ -82,10 +76,9 @@ public class RequestTest {
@Test
public void required_param_is_missing() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'required_param' parameter is missing");
-
- underTest.mandatoryParam("required_param");
+ assertThatThrownBy(() -> underTest.mandatoryParam("required_param"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The 'required_param' parameter is missing");
}
@Test
@@ -118,10 +111,9 @@ public class RequestTest {
String parameter = "maximum_length_param";
defineParameterTestAction(newParam -> newParam.setMaximumLength(10), parameter);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(format("'%s' length (11) is longer than the maximum authorized (10)", parameter));
-
- underTest.setParam(parameter, repeat("X", 11)).param(parameter);
+ assertThatThrownBy(() -> underTest.setParam(parameter, repeat("X", 11)).param(parameter))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(format("'%s' length (11) is longer than the maximum authorized (10)", parameter));
}
@Test
@@ -140,10 +132,9 @@ public class RequestTest {
String param = "maximum_value_param";
defineParameterTestAction(newParam -> newParam.setMaximumValue(10), param);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(format("'%s' value (11) must be less than 10", param));
-
- underTest.setParam(param, "11").param(param);
+ assertThatThrownBy(() -> underTest.setParam(param, "11").param(param))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(format("'%s' value (11) must be less than 10", param));
}
@Test
@@ -151,10 +142,9 @@ public class RequestTest {
String param = "maximum_value_param";
defineParameterTestAction(newParam -> newParam.setMaximumValue(10), param);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'maximum_value_param' value 'foo' cannot be parsed as an integer");
-
- underTest.setParam(param, "foo").paramAsInt(param);
+ assertThatThrownBy(() -> underTest.setParam(param, "foo").paramAsInt(param))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("'maximum_value_param' value 'foo' cannot be parsed as an integer");
}
@Test
@@ -166,10 +156,9 @@ public class RequestTest {
@Test
public void fail_if_no_required_param_as_strings() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'a_required_string' parameter is missing");
-
- underTest.mandatoryParamAsStrings("a_required_string");
+ assertThatThrownBy(() -> underTest.mandatoryParamAsStrings("a_required_string"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The 'a_required_string' parameter is missing");
}
@Test
@@ -184,10 +173,9 @@ public class RequestTest {
public void fail_when_multi_param_has_more_values_than_maximum_values() {
underTest.setMultiParam("has_maximum_values", newArrayList("firstValue", "secondValue", "thirdValue"));
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'has_maximum_values' can contains only 2 values, got 3");
-
- underTest.multiParam("has_maximum_values");
+ assertThatThrownBy(() -> underTest.multiParam("has_maximum_values"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("'has_maximum_values' can contains only 2 values, got 3");
}
@Test
@@ -201,10 +189,9 @@ public class RequestTest {
@Test
public void fail_when_no_multi_param() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'a_required_multi_param' parameter is missing");
-
- underTest.mandatoryMultiParam("a_required_multi_param");
+ assertThatThrownBy(() -> underTest.mandatoryMultiParam("a_required_multi_param"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The 'a_required_multi_param' parameter is missing");
}
@Test
@@ -242,10 +229,9 @@ public class RequestTest {
@Test
public void fail_when_param_is_not_an_int() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'a_number' parameter cannot be parsed as an integer value: not-an-int");
-
- assertThat(underTest.setParam("a_number", "not-an-int").paramAsInt("a_number")).isEqualTo(123);
+ assertThatThrownBy(() -> underTest.setParam("a_number", "not-an-int").paramAsInt("a_number"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The 'a_number' parameter cannot be parsed as an integer value: not-an-int");
}
@Test
@@ -255,10 +241,9 @@ public class RequestTest {
@Test
public void fail_when_param_is_not_a_long() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'a_number' parameter cannot be parsed as a long value: not_a_long");
-
- underTest.setParam("a_number", "not_a_long").paramAsLong("a_number");
+ assertThatThrownBy(() -> underTest.setParam("a_number", "not_a_long").paramAsLong("a_number"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The 'a_number' parameter cannot be parsed as a long value: not_a_long");
}
@Test
@@ -271,10 +256,9 @@ public class RequestTest {
@Test
public void fail_if_incorrect_param_as_boolean() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Property a_boolean is not a boolean value: oui");
-
- underTest.setParam("a_boolean", "oui").paramAsBoolean("a_boolean");
+ assertThatThrownBy(() -> underTest.setParam("a_boolean", "oui").paramAsBoolean("a_boolean"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Property a_boolean is not a boolean value: oui");
}
@Test
@@ -295,10 +279,9 @@ public class RequestTest {
@Test
public void fail_when_param_as_enums_has_more_values_than_maximum_values() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'has_maximum_values' can contains only 2 values, got 3");
-
- underTest.setParam("has_maximum_values", "BETA,READY,REMOVED").paramAsEnums("has_maximum_values", RuleStatus.class);
+ assertThatThrownBy(() -> underTest.setParam("has_maximum_values", "BETA,READY,REMOVED").paramAsEnums("has_maximum_values", RuleStatus.class))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("'has_maximum_values' can contains only 2 values, got 3");
}
@Test
@@ -535,18 +518,16 @@ public class RequestTest {
@Test
public void fail_when_param_as_date_not_a_date() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The date 'polop' does not respect format 'yyyy-MM-dd'");
-
- underTest.setParam("a_date", "polop").paramAsDate("a_date");
+ assertThatThrownBy(() -> underTest.setParam("a_date", "polop").paramAsDate("a_date"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The date 'polop' does not respect format 'yyyy-MM-dd'");
}
@Test
public void fail_when_param_as_datetime_not_a_datetime() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'polop' cannot be parsed as either a date or date+time");
-
- underTest.setParam("a_datetime", "polop").paramAsDateTime("a_datetime");
+ assertThatThrownBy(() -> underTest.setParam("a_datetime", "polop").paramAsDateTime("a_datetime"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("'polop' cannot be parsed as either a date or date+time");
}
@Test
@@ -560,10 +541,9 @@ public class RequestTest {
@Test
public void fail_when_param_as_strings_has_more_values_than_maximum_values() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'has_maximum_values' can contains only 2 values, got 3");
-
- underTest.setParam("has_maximum_values", "foo,bar,baz").paramAsStrings("has_maximum_values");
+ assertThatThrownBy(() -> underTest.setParam("has_maximum_values", "foo,bar,baz").paramAsStrings("has_maximum_values"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("'has_maximum_values' can contains only 2 values, got 3");
}
@Test
@@ -573,18 +553,16 @@ public class RequestTest {
@Test
public void fail_if_param_is_not_defined() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("BUG - parameter 'unknown' is undefined for action 'my_action'");
-
- underTest.param("unknown");
+ assertThatThrownBy(() -> underTest.param("unknown"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("BUG - parameter 'unknown' is undefined for action 'my_action'");
}
@Test
public void fail_if_multi_param_is_not_defined() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Parameter 'unknown' not found for action 'my_action'");
-
- underTest.multiParam("unknown");
+ assertThatThrownBy(() -> underTest.multiParam("unknown"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Parameter 'unknown' not found for action 'my_action'");
}
@Test
@@ -597,10 +575,9 @@ public class RequestTest {
public void fail_if_not_a_possible_value() {
underTest.setParam("has_possible_values", "not_possible");
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Value of parameter 'has_possible_values' (not_possible) must be one of: [foo, bar]");
-
- underTest.param("has_possible_values");
+ assertThatThrownBy(() -> underTest.param("has_possible_values"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Value of parameter 'has_possible_values' (not_possible) must be one of: [foo, bar]");
}
@Test
@@ -624,10 +601,9 @@ public class RequestTest {
@Test
public void mandatory_param_as_part() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'required_param' parameter is missing");
-
- underTest.mandatoryParamAsPart("required_param");
+ assertThatThrownBy(() -> underTest.mandatoryParamAsPart("required_param"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("The 'required_param' parameter is missing");
}
private void defineParameterTestAction(Consumer<WebService.NewParam> newParam, String parameter) {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java
index e12d668a3ea..5c2573e7a12 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java
@@ -26,7 +26,6 @@ import java.util.Collections;
import org.apache.commons.lang.StringUtils;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.server.ws.WebService.NewController;
@@ -34,6 +33,7 @@ import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
@@ -41,8 +41,6 @@ import static org.mockito.Mockito.mock;
public class WebServiceTest {
@Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
public LogTester logTester = new LogTester();
private WebService.Context context = new WebService.Context();
@@ -96,75 +94,69 @@ public class WebServiceTest {
@Test
public void fail_if_duplicated_ws_keys() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("The web service 'api/metric' is defined multiple times");
-
MetricWs metricWs = new MetricWs();
metricWs.define(context);
- ((WebService) context -> {
+
+ assertThatThrownBy(() -> ((WebService) context -> {
NewController newController = context.createController("api/metric");
newDefaultAction(newController, "delete");
newController.done();
- }).define(context);
+ }).define(context))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("The web service 'api/metric' is defined multiple times");
}
@Test
public void fail_if_no_action_handler() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("RequestHandler is not set on action rule/show");
-
- ((WebService) context -> {
+ assertThatThrownBy(() -> ((WebService) context -> {
NewController controller = context.createController("rule");
newDefaultAction(controller, "show")
.setHandler(null);
controller.done();
- }).define(context);
+ }).define(context))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("RequestHandler is not set on action rule/show");
}
@Test
public void fail_if_duplicated_action_keys() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("The action 'delete' is defined multiple times in the web service 'rule'");
-
- ((WebService) context -> {
+ assertThatThrownBy(() -> ((WebService) context -> {
NewController newController = context.createController("rule");
newDefaultAction(newController, "create");
newDefaultAction(newController, "delete");
newDefaultAction(newController, "delete");
newController.done();
- }).define(context);
+ }).define(context))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("The action 'delete' is defined multiple times in the web service 'rule'");
}
@Test
public void fail_if_no_actions() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("At least one action must be declared in the web service 'rule'");
-
- ((WebService) context -> context.createController("rule").done()).define(context);
+ assertThatThrownBy(() -> ((WebService) context -> context.createController("rule").done()).define(context))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("At least one action must be declared in the web service 'rule'");
}
@Test
public void fail_if_no_controller_path() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("WS controller path must not be empty");
-
- ((WebService) context -> context.createController(null).done()).define(context);
+ assertThatThrownBy(() -> ((WebService) context -> context.createController(null).done()).define(context))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("WS controller path must not be empty");
}
@Test
public void controller_path_must_not_start_with_slash() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("WS controller path must not start or end with slash: /hello");
-
- ((WebService) context -> context.createController("/hello").done()).define(context);
+ assertThatThrownBy(() -> ((WebService) context -> context.createController("/hello").done()).define(context))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("WS controller path must not start or end with slash: /hello");
}
@Test
public void controller_path_must_not_end_with_slash() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("WS controller path must not start or end with slash: hello/");
-
- ((WebService) context -> context.createController("hello/").done()).define(context);
+ assertThatThrownBy(() -> ((WebService) context -> context.createController("hello/").done()).define(context))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("WS controller path must not start or end with slash: hello/");
}
@Test
@@ -345,28 +337,27 @@ public class WebServiceTest {
@Test
public void fail_if_required_param_has_default_value() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Default value must not be set on parameter 'api/rule/create?key' as it's marked as required");
- ((WebService) context -> {
+ assertThatThrownBy(() -> ((WebService) context -> {
NewController controller = context.createController("api/rule");
NewAction action = newDefaultAction(controller, "create");
action.createParam("key").setRequired(true).setDefaultValue("abc");
controller.done();
- }).define(context);
+ }).define(context))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Default value must not be set on parameter 'api/rule/create?key' as it's marked as required");
}
@Test
public void fail_if_duplicated_action_parameters() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("The parameter 'key' is defined multiple times in the action 'create'");
-
- ((WebService) context -> {
+ assertThatThrownBy(() -> ((WebService) context -> {
NewController controller = context.createController("api/rule");
NewAction action = newDefaultAction(controller, "create");
action.createParam("key");
action.createParam("key");
controller.done();
- }).define(context);
+ }).define(context))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("The parameter 'key' is defined multiple times in the action 'create'");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/task/TaskDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/task/TaskDefinitionTest.java
index 7af68daf949..a844a754c67 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/task/TaskDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/task/TaskDefinitionTest.java
@@ -19,17 +19,13 @@
*/
package org.sonar.api.task;
-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 TaskDefinitionTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void should_build() {
TaskDefinition def = TaskDefinition.builder().key("foo").taskClass(FooTask.class).description("Foo").build();
@@ -67,30 +63,30 @@ public class TaskDefinitionTest {
@Test
public void description_should_be_required() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Description must be set for task 'foo'");
- TaskDefinition.builder().key("foo").taskClass(FooTask.class).build();
+ assertThatThrownBy(() -> TaskDefinition.builder().key("foo").taskClass(FooTask.class).build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Description must be set for task 'foo'");
}
@Test
public void key_should_be_required() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Task key must be set");
- TaskDefinition.builder().description("Foo").taskClass(FooTask.class).build();
+ assertThatThrownBy(() -> TaskDefinition.builder().description("Foo").taskClass(FooTask.class).build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Task key must be set");
}
@Test
public void key_should_not_contain_spaces() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Task key 'fo o' must match " + TaskDefinition.KEY_PATTERN);
- TaskDefinition.builder().key("fo o").description("foo").taskClass(FooTask.class).build();
+ assertThatThrownBy(() -> TaskDefinition.builder().key("fo o").description("foo").taskClass(FooTask.class).build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Task key 'fo o' must match " + TaskDefinition.KEY_PATTERN);
}
@Test
public void class_should_be_required() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Class must be set for task 'foo'");
- TaskDefinition.builder().key("foo").description("Foo").build();
+ assertThatThrownBy(() -> TaskDefinition.builder().key("foo").description("Foo").build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Class must be set for task 'foo'");
}
private static class FooTask implements Task {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/user/UserGroupValidationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/user/UserGroupValidationTest.java
index 37e04a25434..6d610794bc8 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/user/UserGroupValidationTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/user/UserGroupValidationTest.java
@@ -20,52 +20,44 @@
package org.sonar.api.user;
import com.google.common.base.Strings;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
-public class UserGroupValidationTest {
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
- @Rule
- public ExpectedException thrown = ExpectedException.none();
+public class UserGroupValidationTest {
@Test
public void fail_when_group_name_is_Anyone() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Anyone group cannot be used");
-
- UserGroupValidation.validateGroupName("AnyOne");
+ assertThatThrownBy(() -> UserGroupValidation.validateGroupName("AnyOne"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Anyone group cannot be used");
}
@Test
public void fail_when_group_name_is_empty() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be empty");
-
- UserGroupValidation.validateGroupName("");
+ assertThatThrownBy(() -> UserGroupValidation.validateGroupName(""))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be empty");
}
@Test
public void fail_when_group_name_contains_only_blank() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be empty");
-
- UserGroupValidation.validateGroupName(" ");
+ assertThatThrownBy(() -> UserGroupValidation.validateGroupName(" "))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be empty");
}
@Test
public void fail_when_group_name_is_too_big() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be longer than 255 characters");
-
- UserGroupValidation.validateGroupName(Strings.repeat("name", 300));
+ assertThatThrownBy(() -> UserGroupValidation.validateGroupName(Strings.repeat("name", 300)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be longer than 255 characters");
}
@Test
public void fail_when_group_name_is_null() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Group name cannot be empty");
-
- UserGroupValidation.validateGroupName(null);
+ assertThatThrownBy(() -> UserGroupValidation.validateGroupName(null))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Group name cannot be empty");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java
index 11c04c3885a..efcab74f464 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java
@@ -26,12 +26,11 @@ import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Date;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.utils.DateUtils.parseDateOrDateTime;
import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime;
import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
@@ -39,9 +38,6 @@ import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
@RunWith(DataProviderRunner.class)
public class DateUtilsTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void parseDate_valid_format() {
Date date = DateUtils.parseDate("2010-05-18");
@@ -50,14 +46,14 @@ public class DateUtilsTest {
@Test
public void parseDate_not_valid_format() {
- expectedException.expect(MessageException.class);
- DateUtils.parseDate("2010/05/18");
+ assertThatThrownBy(() -> DateUtils.parseDate("2010/05/18"))
+ .isInstanceOf(MessageException.class);
}
@Test
public void parseDate_not_lenient() {
- expectedException.expect(MessageException.class);
- DateUtils.parseDate("2010-13-18");
+ assertThatThrownBy(() -> DateUtils.parseDate("2010-13-18"))
+ .isInstanceOf(MessageException.class);
}
@Test
@@ -69,8 +65,8 @@ public class DateUtilsTest {
@Test
public void parseDate_fail_if_additional_characters() {
- expectedException.expect(MessageException.class);
- DateUtils.parseDate("1986-12-04foo");
+ assertThatThrownBy(() -> DateUtils.parseDate("1986-12-04foo"))
+ .isInstanceOf(MessageException.class);
}
@Test
@@ -81,14 +77,14 @@ public class DateUtilsTest {
@Test
public void parseDateTime_not_valid_format() {
- expectedException.expect(MessageException.class);
- DateUtils.parseDate("2010/05/18 10:55");
+ assertThatThrownBy(() -> DateUtils.parseDate("2010/05/18 10:55"))
+ .isInstanceOf(MessageException.class);
}
@Test
public void parseDateTime_fail_if_additional_characters() {
- expectedException.expect(MessageException.class);
- DateUtils.parseDateTime("1986-12-04T01:02:03+0300foo");
+ assertThatThrownBy(() -> DateUtils.parseDate("1986-12-04T01:02:03+0300foo"))
+ .isInstanceOf(MessageException.class);
}
@Test
@@ -139,7 +135,7 @@ public class DateUtilsTest {
@DataProvider
public static Object[][] date_times() {
- return new Object[][] {
+ return new Object[][]{
{"2014-05-27", Date.from(LocalDate.parse("2014-05-27").atStartOfDay(ZoneId.systemDefault()).toInstant())},
{"2014-05-27T15:50:45+0100", Date.from(OffsetDateTime.parse("2014-05-27T15:50:45+01:00").toInstant())},
{null, null}
@@ -177,26 +173,23 @@ public class DateUtilsTest {
@Test
public void fail_when_param_as_date_or_datetime_not_a_datetime() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Date 'polop' cannot be parsed as either a date or date+time");
-
- parseDateOrDateTime("polop");
+ assertThatThrownBy(() -> parseDateOrDateTime("polop"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Date 'polop' cannot be parsed as either a date or date+time");
}
@Test
public void fail_when_param_as_starting_datetime_not_a_datetime() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Date 'polop' cannot be parsed as either a date or date+time");
-
- parseStartingDateOrDateTime("polop");
+ assertThatThrownBy(() -> parseStartingDateOrDateTime("polop"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Date 'polop' cannot be parsed as either a date or date+time");
}
@Test
public void fail_when_param_as_ending_datetime_not_a_datetime() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("'polop' cannot be parsed as either a date or date+time");
-
- parseEndingDateOrDateTime("polop");
+ assertThatThrownBy(() -> parseEndingDateOrDateTime("polop"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("'polop' cannot be parsed as either a date or date+time");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/PagingTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/PagingTest.java
index 6daf40c2734..c761ffb58fe 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/PagingTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/PagingTest.java
@@ -19,16 +19,13 @@
*/
package org.sonar.api.utils;
-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.sonar.api.utils.Paging.forPageIndex;
public class PagingTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void test_pagination() {
@@ -58,25 +55,22 @@ public class PagingTest {
@Test
public void page_size_should_be_strictly_positive() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Page size must be strictly positive. Got 0");
-
- forPageIndex(5).withPageSize(0).andTotal(5);
+ assertThatThrownBy(() -> forPageIndex(5).withPageSize(0).andTotal(5))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Page size must be strictly positive. Got 0");
}
@Test
public void page_index_should_be_strictly_positive() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Page index must be strictly positive. Got 0");
-
- forPageIndex(0).withPageSize(5).andTotal(5);
+ assertThatThrownBy(() -> forPageIndex(0).withPageSize(5).andTotal(5))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Page index must be strictly positive. Got 0");
}
@Test
public void total_items_should_be_positive() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Total items must be positive. Got -1");
-
- forPageIndex(5).withPageSize(5).andTotal(-1);
+ assertThatThrownBy(() -> forPageIndex(5).withPageSize(5).andTotal(-1))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Total items must be positive. Got -1");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java
index dcbf8cc93e2..6ea81060392 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java
@@ -19,16 +19,14 @@
*/
package org.sonar.api.utils;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
+import org.junit.Before;
+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;
import static org.mockito.Mockito.when;
@@ -36,9 +34,6 @@ public class UriReaderTest {
private static URI testFile;
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Before
public void init() throws URISyntaxException {
testFile = UriReaderTest.class.getResource("/org/sonar/api/utils/UriReaderTest/foo.txt").toURI();
@@ -65,16 +60,17 @@ public class UriReaderTest {
@Test
public void file_readString_fails_if_file_not_found() throws Exception {
- thrown.expect(RuntimeException.class);
UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]);
- uriReader.readString(new URI("file:/notfound"), StandardCharsets.UTF_8);
+
+ assertThatThrownBy(() -> uriReader.readString(new URI("file:/notfound"), StandardCharsets.UTF_8))
+ .isInstanceOf(RuntimeException.class);
}
@Test
public void file_readBytes_fails_if_file_not_found() throws Exception {
- thrown.expect(RuntimeException.class);
UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]);
- uriReader.readBytes(new URI("file:/notfound"));
+ assertThatThrownBy(() -> uriReader.readBytes(new URI("file:/notfound")))
+ .isInstanceOf(RuntimeException.class);
}
@Test
@@ -87,10 +83,11 @@ public class UriReaderTest {
}
@Test
- public void fail_if_unknown_scheme() throws Exception {
- thrown.expect(IllegalArgumentException.class);
+ public void fail_if_unknown_scheme() {
UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]);
- uriReader.readBytes(new URI("ftp://sonarsource.org"));
+
+ assertThatThrownBy(() -> uriReader.readBytes(new URI("ftp://sonarsource.org")))
+ .isInstanceOf(RuntimeException.class);
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/VersionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/VersionTest.java
index c7b904292e6..ed2907cd713 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/VersionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/VersionTest.java
@@ -19,18 +19,14 @@
*/
package org.sonar.api.utils;
-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.sonar.api.utils.Version.parse;
public class VersionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void test_parse() {
assertVersion(parse(""), 0, 0, 0, 0, "");
@@ -46,10 +42,9 @@ public class VersionTest {
@Test
public void parse_throws_IAE_if_more_than_4_fields() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Maximum 4 fields are accepted: 1.2.3.456.7");
-
- parse("1.2.3.456.7");
+ assertThatThrownBy(() -> parse("1.2.3.456.7"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Maximum 4 fields are accepted: 1.2.3.456.7");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/ZipUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/ZipUtilsTest.java
index bcdad396650..1a0684cd9e4 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/ZipUtilsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/ZipUtilsTest.java
@@ -32,17 +32,15 @@ import org.apache.commons.io.FileUtils;
import org.assertj.core.util.Files;
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;
public class ZipUtilsTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void zip_directory() throws IOException {
@@ -96,10 +94,10 @@ public class ZipUtilsTest {
File zip = new File(getClass().getResource("ZipUtilsTest/zip-slip.zip").toURI());
File toDir = temp.newFolder();
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt");
-
- ZipUtils.unzip(zip, toDir);
+ assertThatThrownBy(() -> ZipUtils.unzip(zip, toDir))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../" +
+ "../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt");
}
@Test
@@ -107,11 +105,12 @@ public class ZipUtilsTest {
File zip = new File(getClass().getResource("ZipUtilsTest/zip-slip.zip").toURI());
File toDir = temp.newFolder();
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt");
try (InputStream input = new FileInputStream(zip)) {
- ZipUtils.unzip(input, toDir);
+ assertThatThrownBy(() -> ZipUtils.unzip(input, toDir))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../.." +
+ "/../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java
index 2a24db6c58c..f72b3b77ba6 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java
@@ -26,12 +26,12 @@ import org.apache.commons.lang.SystemUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;
import org.sonar.api.utils.System2;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.fail;
public class CommandExecutorTest {
@@ -42,9 +42,6 @@ public class CommandExecutorTest {
@Rule
public TestName testName = new TestName();
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
private File workDir;
@Before
@@ -76,17 +73,19 @@ public class CommandExecutorTest {
@Test(timeout = 3000L)
public void stdOut_consumer_can_throw_exception() throws Exception {
Command command = Command.create(getScript("output")).setDirectory(workDir);
- thrown.expect(CommandException.class);
- thrown.expectMessage("Error inside stdOut stream");
- CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L);
+
+ assertThatThrownBy(() -> CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L))
+ .isInstanceOf(CommandException.class)
+ .hasMessageContaining("Error inside stdOut stream");
}
@Test(timeout = 3000L)
public void stdErr_consumer_can_throw_exception() throws Exception {
Command command = Command.create(getScript("output")).setDirectory(workDir);
- thrown.expect(CommandException.class);
- thrown.expectMessage("Error inside stdErr stream");
- CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1500L);
+
+ assertThatThrownBy(() -> CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1500L))
+ .isInstanceOf(CommandException.class)
+ .hasMessageContaining("Error inside stdErr stream");
}
private static final StreamConsumer NOP_CONSUMER = line -> {
@@ -136,8 +135,8 @@ public class CommandExecutorTest {
@Test
public void should_fail_if_script_not_found() {
- thrown.expect(CommandException.class);
- CommandExecutor.create().execute(Command.create("notfound").setDirectory(workDir), 1000L);
+ assertThatThrownBy(() -> CommandExecutor.create().execute(Command.create("notfound").setDirectory(workDir), 1000L))
+ .isInstanceOf(CommandException.class);
}
private static String getScript(String name) throws IOException {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
index 2f1f89ceb79..dff18017318 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
@@ -20,33 +20,28 @@
package org.sonar.api.utils.command;
import com.google.common.collect.ImmutableMap;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.utils.System2;
-
import java.io.File;
import java.util.Arrays;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
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 CommandTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void fail_if_blank_executable() {
- thrown.expect(IllegalArgumentException.class);
- Command.create(" ");
+ assertThatThrownBy(() -> Command.create(" "))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void fail_if_null_executable() {
- thrown.expect(IllegalArgumentException.class);
- Command.create(null);
+ assertThatThrownBy(() -> Command.create(null))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java
index 9ae97fcfa35..c18bf2f7b40 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java
@@ -26,12 +26,11 @@ import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.utils.DateUtils;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -40,9 +39,6 @@ public class JsonWriterTest {
private static final String EMPTY_STRING = "";
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
private StringWriter stringWriter = new StringWriter();
private JsonWriter underTest = JsonWriter.of(stringWriter);
@@ -198,24 +194,24 @@ public class JsonWriterTest {
@Test
public void fail_on_NaN_value() {
- expectedException.expect(WriterException.class);
- underTest.beginObject().prop("foo", Double.NaN).endObject().close();
+ assertThatThrownBy(() -> underTest.beginObject().prop("foo", Double.NaN).endObject().close())
+ .isInstanceOf(WriterException.class);
}
@Test
public void fail_if_not_valid() {
- expectedException.expect(WriterException.class);
- underTest.beginObject().endArray().close();
+ assertThatThrownBy(() -> underTest.beginObject().endArray().close())
+ .isInstanceOf(WriterException.class);
}
@Test
public void fail_to_begin_array() throws Exception {
com.google.gson.stream.JsonWriter gson = mock(com.google.gson.stream.JsonWriter.class);
when(gson.beginArray()).thenThrow(new IOException("the reason"));
- expectedException.expect(WriterException.class);
- expectedException.expectMessage("Fail to write JSON");
- new JsonWriter(gson).beginArray();
+ assertThatThrownBy(() -> new JsonWriter(gson).beginArray())
+ .isInstanceOf(WriterException.class)
+ .hasMessage("Fail to write JSON");
}
private void expect(String s) {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/XmlWriterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/XmlWriterTest.java
index ba0a3d4127c..d257603de95 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/XmlWriterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/XmlWriterTest.java
@@ -19,17 +19,13 @@
*/
package org.sonar.api.utils.text;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
import java.io.StringWriter;
+import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class XmlWriterTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
StringWriter xml = new StringWriter();
XmlWriter writer = XmlWriter.of(xml);
@@ -79,8 +75,8 @@ public class XmlWriterTest {
@Test
public void fail_on_NaN_value() {
- thrown.expect(WriterException.class);
- thrown.expectMessage("Fail to write XML. Double value is not valid: NaN");
- writer.begin("root").prop("foo", Double.NaN).end().close();
+ assertThatThrownBy(() -> writer.begin("root").prop("foo", Double.NaN).end().close())
+ .isInstanceOf(WriterException.class)
+ .hasMessage("Fail to write XML. Double value is not valid: NaN");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java
index 5fe2fdfc1b8..f1a6e48e2ff 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java
@@ -23,15 +23,12 @@ import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
-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 ServletFilterTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Test
public void include_all() {
@@ -188,9 +185,9 @@ public class ServletFilterTest {
@Test
public void create_throws_IAE_if_empty_url() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("URL pattern must start with slash '/': ");
- ServletFilter.UrlPattern.create("");
+ assertThatThrownBy(() -> ServletFilter.UrlPattern.create(""))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("URL pattern must start with slash '/': ");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/page/ContextTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/page/ContextTest.java
index ba4f05c004d..9d06dc0df31 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/web/page/ContextTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/page/ContextTest.java
@@ -20,16 +20,13 @@
package org.sonar.api.web.page;
import java.util.Collection;
-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.assertj.core.api.Assertions.tuple;
public class ContextTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private Context underTest = new Context();
@@ -39,10 +36,9 @@ public class ContextTest {
public void no_pages_with_the_same_path() {
underTest.addPage(page);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Page 'Project Export' cannot be loaded. Another page with key 'governance/project_export' already exists.");
-
- underTest.addPage(page);
+ assertThatThrownBy(() -> underTest.addPage(page))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Page 'Project Export' cannot be loaded. Another page with key 'governance/project_export' already exists.");
}
@Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/page/PageTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/page/PageTest.java
index 19b67837f93..2b4bf64d157 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/web/page/PageTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/page/PageTest.java
@@ -19,13 +19,12 @@
*/
package org.sonar.api.web.page;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.web.page.Page.Qualifier;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.web.page.Page.Qualifier.APP;
import static org.sonar.api.web.page.Page.Qualifier.MODULE;
import static org.sonar.api.web.page.Page.Qualifier.PROJECT;
@@ -35,8 +34,6 @@ import static org.sonar.api.web.page.Page.Scope.COMPONENT;
import static org.sonar.api.web.page.Page.Scope.GLOBAL;
public class PageTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private Page.Builder underTest = Page.builder("governance/project_dump").setName("Project Dump");
@@ -96,59 +93,51 @@ public class PageTest {
@Test
public void fail_if_null_qualifiers() {
- expectedException.expect(NullPointerException.class);
-
- underTest.setComponentQualifiers((Qualifier[])null).build();
+ assertThatThrownBy(() -> underTest.setComponentQualifiers((Qualifier[])null).build())
+ .isInstanceOf(NullPointerException.class);
}
@Test
public void fail_if_a_page_has_a_null_key() {
- expectedException.expect(NullPointerException.class);
-
- Page.builder(null).setName("Say my name").build();
+ assertThatThrownBy(() -> Page.builder(null).setName("Say my name").build())
+ .isInstanceOf(NullPointerException.class);
}
@Test
public void fail_if_a_page_has_an_empty_key() {
- expectedException.expect(IllegalArgumentException.class);
-
- Page.builder("").setName("Say my name").build();
+ assertThatThrownBy(() -> Page.builder("").setName("Say my name").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void fail_if_a_page_has_a_null_name() {
- expectedException.expect(IllegalArgumentException.class);
-
- Page.builder("governance/project_dump").build();
+ assertThatThrownBy(() -> Page.builder("governance/project_dump").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void fail_if_a_page_has_an_empty_name() {
- expectedException.expect(IllegalArgumentException.class);
-
- Page.builder("governance/project_dump").setName("").build();
+ assertThatThrownBy(() -> Page.builder("governance/project_dump").setName("").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void fail_if_qualifiers_without_scope() {
- expectedException.expect(IllegalArgumentException.class);
-
- underTest.setComponentQualifiers(PROJECT).build();
+ assertThatThrownBy(() -> underTest.setComponentQualifiers(PROJECT).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
public void fail_if_key_does_not_contain_a_slash() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Page key [project_dump] is not valid. It must contain a single slash, for example my_plugin/my_page.");
-
- Page.builder("project_dump").setName("Project Dump").build();
+ assertThatThrownBy(() -> Page.builder("project_dump").setName("Project Dump").build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Page key [project_dump] is not valid. It must contain a single slash, for example my_plugin/my_page.");
}
@Test
public void fail_if_key_contains_more_than_one_slash() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Page key [governance/project/dump] is not valid. It must contain a single slash, for example my_plugin/my_page.");
-
- Page.builder("governance/project/dump").setName("Project Dump").build();
+ assertThatThrownBy(() -> Page.builder("governance/project/dump").setName("Project Dump").build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Page key [governance/project/dump] is not valid. It must contain a single slash, for example my_plugin/my_page.");
}
}