]> source.dussan.org Git - sonarqube.git/commitdiff
Don't use fail() in a try/catch block
authorPhilippe Perrin <philippe.perrin@sonarsource.com>
Tue, 8 Dec 2020 09:57:03 +0000 (10:57 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 9 Dec 2020 20:07:21 +0000 (20:07 +0000)
server/sonar-webserver-auth/src/test/java/org/sonar/server/user/DoPrivilegedTest.java

index 10f1e7f6eee0f4cd76491fdb17cd4697b543890b..238dfdac4c2fa0beea3c1dfcd403fdc340f6070d 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.user;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.db.component.ComponentDto;
@@ -65,17 +66,14 @@ public class DoPrivilegedTest {
       }
     };
 
-    try {
-      DoPrivileged.execute(catcher);
-      fail("An exception should have been raised!");
-    } catch (Throwable ignored) {
-      // verify session in place after task is done
-      assertThat(threadLocalUserSession.get()).isSameAs(session);
+    Assert.assertThrows(Exception.class, () -> DoPrivileged.execute(catcher));
 
-      // verify the session used inside Privileged task
-      assertThat(catcher.userSession.isLoggedIn()).isFalse();
-      assertThat(catcher.userSession.hasComponentPermission("any permission", new ComponentDto())).isTrue();
-    }
+    // verify session in place after task is done
+    assertThat(threadLocalUserSession.get()).isSameAs(session);
+
+    // verify the session used inside Privileged task
+    assertThat(catcher.userSession.isLoggedIn()).isFalse();
+    assertThat(catcher.userSession.hasComponentPermission("any permission", new ComponentDto())).isTrue();
   }
 
   private class UserSessionCatcherTask extends DoPrivileged.Task {