IG: Add option to ignore methods with throws (IDEA-160163)

in "JUnit test method without any assertions" inspection
This commit is contained in:
Bas Leijdekkers
2017-12-31 17:39:03 +01:00
parent f5f360bc6e
commit 171f715eef
5 changed files with 26 additions and 9 deletions
@@ -20,6 +20,7 @@ public class TestMethodWithoutAssertionInspectionBase extends BaseInspection {
protected final MethodMatcher methodMatcher;
@SuppressWarnings("PublicField") public boolean assertKeywordIsAssertion;
@SuppressWarnings("PublicField") public boolean ignoreIfExceptionThrown;
public TestMethodWithoutAssertionInspectionBase() {
methodMatcher = new MethodMatcher(false, "assertionMethods")
@@ -84,6 +85,9 @@ public class TestMethodWithoutAssertionInspectionBase extends BaseInspection {
if (TestUtils.hasExpectedExceptionAnnotation(method)) {
return;
}
if (ignoreIfExceptionThrown && method.getThrowsList().getReferenceElements().length > 0) {
return;
}
if (containsAssertion(method)) {
return;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2017 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package com.siyeh.ig.junit;
import com.intellij.codeInspection.ui.ListTable;
import com.intellij.codeInspection.ui.ListWrappingTableModel;
import com.intellij.util.ui.CheckBox;
import com.intellij.util.ui.FormBuilder;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.ui.UiUtils;
@@ -29,15 +30,17 @@ public class TestMethodWithoutAssertionInspection extends TestMethodWithoutAsser
@Override
public JComponent createOptionsPanel() {
final JPanel panel = new JPanel(new BorderLayout());
final ListTable table = new ListTable(
new ListWrappingTableModel(Arrays.asList(methodMatcher.getClassNames(), methodMatcher.getMethodNamePatterns()), "Assertion class name",
InspectionGadgetsBundle.message("method.name.regex")));
final JPanel tablePanel = UiUtils.createAddRemoveTreeClassChooserPanel(table, "Choose assertion class");
final CheckBox checkBox =
final CheckBox checkBox1 =
new CheckBox(InspectionGadgetsBundle.message("assert.keyword.is.considered.an.assertion"), this, "assertKeywordIsAssertion");
panel.add(tablePanel, BorderLayout.CENTER);
panel.add(checkBox, BorderLayout.SOUTH);
return panel;
final CheckBox checkBox2 =
new CheckBox("Ignore test methods which declare exceptions", this, "ignoreIfExceptionThrown");
return new FormBuilder()
.addComponentFillVertically(UiUtils.createAddRemoveTreeClassChooserPanel(table, "Choose assertion class"), 0)
.addComponent(checkBox1)
.addComponent(checkBox2)
.getPanel();
}
}
@@ -8,7 +8,10 @@ Use the table below to specify which combinations of fully qualified class name
qualify as assertions.
Class names also match subclasses.
<p>
Use the checkbox below to specify if Java <b>assert</b> statements, using the <b>assert</b> keyword, should be considered an assertion.
Use the first checkbox below to specify if Java <b>assert</b> statements, using the <b>assert</b> keyword, should be considered an assertion.
<p>
Use the second checkbox to ignore test methods which declare exceptions.
This can be useful when you have tests that will throw an exception on failure and thus don't need any assertions.
<p>
</body>
@@ -67,4 +67,9 @@ public class TestMethodWithoutAssertion extends TestCase
messageDAO.toString();
}};
}
@Test
public void testMethodWhichThrowsExceptionOnFailure() throws AssertionError {
if (true) throw new AssertionError();
}
}
@@ -59,6 +59,8 @@ public class TestMethodWithoutAssertionInspectionTest extends LightInspectionTes
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new TestMethodWithoutAssertionInspection();
final TestMethodWithoutAssertionInspection inspection = new TestMethodWithoutAssertionInspection();
inspection.ignoreIfExceptionThrown = true;
return inspection;
}
}