Finding unused methods in IntelliJ (excluding tests)
Asked Answered
L

2

13

I ran into a method today that is .. not used anywhere .. but is tested. Since it is used by a test, IntelliJ did not flag the method as 'unused'.

Does IntelliJ allow for the following search condition "Find methods that are unused with the exception of tests, where test is defined as anything with @Test annotation"

Levitation answered 11/12, 2012 at 19:25 Comment(1)
This is really annoying. I don't want my declerations to be only used in tests. I want to be warned about them if they are only used in tests.Cleanser
C
4

There is a related feature request:

  • IDEA-56519 Inspection for unused code needs options to take into account methods run by test harnesses

While the first point is already covered by the Configure annotations... option in the inspection settings, the second point is still valid:

An option such that a test method (as described above) is not considered an entry point. This is so that we can eliminate ‘production’ methods that are only used by test methods.

Celandine answered 11/12, 2012 at 23:24 Comment(0)
B
9

This feature works only for batch inspection and disabled in the editor.¹

According to the JetBrains IntelliJ IDEA 2016.3 EAP Makes Unused Code Detection More Flexible blog, it's now possible.

You can find this in Settings > Editor > Inspections > Java > Declaration redundancy > Unused declaration > on the right.

Configuration option to exclude test entry points


¹ Anna Kozlova, comment in said blog post, January 11, 2017

Beatup answered 7/7, 2020 at 11:20 Comment(0)
C
4

There is a related feature request:

  • IDEA-56519 Inspection for unused code needs options to take into account methods run by test harnesses

While the first point is already covered by the Configure annotations... option in the inspection settings, the second point is still valid:

An option such that a test method (as described above) is not considered an entry point. This is so that we can eliminate ‘production’ methods that are only used by test methods.

Celandine answered 11/12, 2012 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.