]> source.dussan.org Git - sonarqube.git/commitdiff
Tests should use ExpectedException
authorDavid Gageot <david@gageot.net>
Wed, 13 May 2015 09:55:37 +0000 (11:55 +0200)
committerDavid Gageot <david@gageot.net>
Wed, 13 May 2015 13:13:54 +0000 (15:13 +0200)
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyNodesStatsRequestBuilderTest.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java

index 1035a9bc7434fbcb6e649b93633703095914d715..7b6861ed42367e7336775649b32365e37b34bfd3 100644 (file)
@@ -24,13 +24,13 @@ import org.elasticsearch.common.unit.TimeValue;
 import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.log.LogTester;
 import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.FakeIndexDefinition;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 
 public class ProxyNodesStatsRequestBuilderTest {
 
@@ -40,6 +40,9 @@ public class ProxyNodesStatsRequestBuilderTest {
   @Rule
   public LogTester logTester = new LogTester();
 
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   @Test
   public void stats() {
     esTester.client().prepareNodesStats().get();
@@ -62,32 +65,26 @@ public class ProxyNodesStatsRequestBuilderTest {
 
   @Test
   public void get_with_string_timeout_is_not_yet_implemented() {
-    try {
-      esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).get("1");
-      fail();
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
-    }
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Not yet implemented");
+
+    esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).get("1");
   }
 
   @Test
   public void get_with_time_value_timeout_is_not_yet_implemented() {
-    try {
-      esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1));
-      fail();
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
-    }
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Not yet implemented");
+
+    esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1));
   }
 
   @Test
   public void execute_should_throw_an_unsupported_operation_exception() {
-    try {
-      esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).execute();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous");
-    }
+    thrown.expect(UnsupportedOperationException.class);
+    thrown.expectMessage("execute() should not be called as it's used for asynchronous");
+
+    esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).execute();
   }
 
 }
index 29cfc369d0a17a99ac5fb89b4e9f44e14a4ba46d..726395963abbc4798f2079372429b14908c10783 100644 (file)
  */
 package org.sonar.api.batch.rule.internal;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.batch.rule.ActiveRule;
 import org.sonar.api.batch.rule.ActiveRules;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.Severity;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 
 public class ActiveRulesBuilderTest {
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   @Test
   public void no_rules() {
     ActiveRulesBuilder builder = new ActiveRulesBuilder();
@@ -81,11 +85,10 @@ public class ActiveRulesBuilderTest {
   public void fail_to_add_twice_the_same_rule() {
     ActiveRulesBuilder builder = new ActiveRulesBuilder();
     builder.create(RuleKey.of("squid", "S0001")).activate();
-    try {
-      builder.create(RuleKey.of("squid", "S0001")).activate();
-      fail();
-    } catch (IllegalStateException e) {
-      assertThat(e).hasMessage("Rule 'squid:S0001' is already activated");
-    }
+
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Rule 'squid:S0001' is already activated");
+
+    builder.create(RuleKey.of("squid", "S0001")).activate();
   }
 }
index 95f0cea83cdc6dbbdee56dfad05f09fa2b1db266..a64550d7b09721a771e8e7d7acbd9947bb795b8b 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.batch.rule.internal;
 
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.batch.debt.DebtRemediationFunction;
 import org.sonar.api.batch.rule.Rule;
 import org.sonar.api.batch.rule.Rules;
@@ -29,9 +30,11 @@ import org.sonar.api.rule.Severity;
 import org.sonar.api.utils.Duration;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 
 public class RulesBuilderTest {
+  @org.junit.Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   @Test
   public void no_rules() {
     RulesBuilder builder = new RulesBuilder();
@@ -98,12 +101,11 @@ public class RulesBuilderTest {
   public void fail_to_add_twice_the_same_rule() {
     RulesBuilder builder = new RulesBuilder();
     builder.add(RuleKey.of("squid", "S0001"));
-    try {
-      builder.add(RuleKey.of("squid", "S0001"));
-      fail();
-    } catch (IllegalStateException e) {
-      assertThat(e).hasMessage("Rule 'squid:S0001' already exists");
-    }
+
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Rule 'squid:S0001' already exists");
+
+    builder.add(RuleKey.of("squid", "S0001"));
   }
 
   @Test
@@ -112,11 +114,10 @@ public class RulesBuilderTest {
     NewRule newRule = builder.add(RuleKey.of("squid", "S0001"));
     newRule.addParam("min");
     newRule.addParam("max");
-    try {
-      newRule.addParam("min");
-      fail();
-    } catch (IllegalStateException e) {
-      assertThat(e).hasMessage("Parameter 'min' already exists on rule 'squid:S0001'");
-    }
+
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Parameter 'min' already exists on rule 'squid:S0001'");
+
+    newRule.addParam("min");
   }
 }
index 52ed7d9f2d9aa20a16a0f5ef7b6a445bd5199bd6..cc1ba978c6b9219c8aece48fc7920925060e4237 100644 (file)
@@ -19,7 +19,9 @@
  */
 package org.sonar.api.config;
 
+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.PropertyField;
@@ -28,9 +30,10 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.AnnotationUtils;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 
 public class PropertyDefinitionTest {
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
 
   @Test
   public void should_override_toString() {
@@ -268,42 +271,34 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_not_authorise_empty_key() {
-    try {
-      PropertyDefinition.builder(null).build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).hasMessage("Key must be set").isInstanceOf(IllegalArgumentException.class);
-    }
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Key must be set");
+
+    PropertyDefinition.builder(null).build();
   }
 
   @Test
   public void should_not_authorize_defining_on_qualifiers_and_hidden() {
-    try {
-      PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).hidden().build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).hasMessage("Cannot be hidden and defining qualifiers on which to display").isInstanceOf(IllegalArgumentException.class);
-    }
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Cannot be hidden and defining qualifiers on which to display");
+
+    PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).hidden().build();
   }
 
   @Test
   public void should_not_authorize_defining_ony_on_qualifiers_and_hidden() {
-    try {
-      PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.FILE).hidden().build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).hasMessage("Cannot be hidden and defining qualifiers on which to display").isInstanceOf(IllegalArgumentException.class);
-    }
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Cannot be hidden and defining qualifiers on which to display");
+
+    PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.FILE).hidden().build();
   }
 
   @Test
   public void should_not_authorize_defining_on_qualifiers_and_only_on_qualifiers() {
-    try {
-      PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).onlyOnQualifiers(Qualifiers.PROJECT).build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).hasMessage("Cannot define both onQualifiers and onlyOnQualifiers").isInstanceOf(IllegalArgumentException.class);
-    }
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Cannot define both onQualifiers and onlyOnQualifiers");
+
+    PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).onlyOnQualifiers(Qualifiers.PROJECT).build();
   }
 
   @Properties(@Property(key = "hello", name = "Hello", defaultValue = "world", description = "desc",
index e1fb1f2bb416cee4eb7c8a29e920fd0a2035a38e..1b065e082e4b9f5eb18fac8aa2a4857cea6f0a4d 100644 (file)
 
 package org.sonar.api.issue.action;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.issue.condition.Condition;
 import org.sonar.api.issue.internal.DefaultIssue;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public class ActionTest {
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
 
   Condition condition1 = mock(Condition.class);
   Condition condition2 = mock(Condition.class);
@@ -49,12 +52,9 @@ public class ActionTest {
 
   @Test
   public void key_should_be_set() {
-    try {
-      new Action("");
-      fail();
-    } catch (Exception e) {
-      assertThat(e).hasMessage("Action key must be set");
-    }
+    thrown.expectMessage("Action key must be set");
+
+    new Action("");
   }
 
   @Test