From 171f715eefbbf7fb57ff074017775110fcc5cca7 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sun, 31 Dec 2017 17:26:59 +0100 Subject: [PATCH] IG: Add option to ignore methods with throws (IDEA-160163) in "JUnit test method without any assertions" inspection --- ...estMethodWithoutAssertionInspectionBase.java | 4 ++++ .../TestMethodWithoutAssertionInspection.java | 17 ++++++++++------- .../TestMethodWithoutAssertion.html | 5 ++++- .../TestMethodWithoutAssertion.java | 5 +++++ ...estMethodWithoutAssertionInspectionTest.java | 4 +++- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java index be3e7c13de22..aa895d56435e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java @@ -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; } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspection.java index 6d5adfb66c11..29342871ce69 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspection.java @@ -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(); } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodWithoutAssertion.html b/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodWithoutAssertion.html index 2ab43c1ae1d5..310592f46b94 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodWithoutAssertion.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodWithoutAssertion.html @@ -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.

-Use the checkbox below to specify if Java assert statements, using the assert keyword, should be considered an assertion. +Use the first checkbox below to specify if Java assert statements, using the assert keyword, should be considered an assertion. +

+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.

diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java index 4c93835931b4..129a92f137f1 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java @@ -67,4 +67,9 @@ public class TestMethodWithoutAssertion extends TestCase messageDAO.toString(); }}; } + + @Test + public void testMethodWhichThrowsExceptionOnFailure() throws AssertionError { + if (true) throw new AssertionError(); + } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java index ccdd97deec99..22cdfba58fdd 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java @@ -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; } } \ No newline at end of file