4

Unused methods have warning for unused and can be removed, e.g. in Eclipse

The method myMethod() from the type MyClass is never used locally

But sometimes you write code with its unit test and afterwards code isn't used (or removed) in production, but method is still used (only) for unit test

How can we find such unused methods which aren't used in real code (only test code)

  • My tests are under tests folder and code under src folder

For example DAO method:

public interface TransactionDao {

   public boolean updateTrasaction(int id);
}

@Repository
public class TransactionDaoImpl implements TransactionDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public boolean updateTrasaction(int id) {
        return (jdbcTemplate.update(... )>0);
    }
}

used only in Test:

@Test   
public void testUpdateTrasaction() {
    Assert.assertEquals(transactionDao.updateTrasaction(1234), true);
}

I'm aware of static analysis tools and Halting Problem, but still, is there solution for this specific requirement?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 1
    Why not just temporarily delete all test packages, and check the unused methods then? Very simple solution without any tool etc. – buræquete Jun 25 '19 at 05:01
  • @buræquete I can consider it, but isn't there a simple way as searching can ignore folders? (I have multiple modules) – Ori Marko Jun 25 '19 at 05:03
  • To find **all** unused methods, you'd always need some tool, whether it is done in Intellij or Eclipse, but none of these tools have some such package ignore feature afaics. Best is to just use that tool plus deletion of test packages to find all *really* unused methods. – buræquete Jun 25 '19 at 05:10
  • 1
    Maybe you can just increase the warning level of your unused code in [Java compiler settings](https://stackoverflow.com/a/19997851/3641067) so that after a build, you can get unused methods as errors, or just warnings? After you delete your tests ofc. Better than getting some silly tool. – buræquete Jun 25 '19 at 05:19
  • it might work by creating a parallel projet for the tests. Include the main project in the test, the method will not be used in the project itself so the warning will be visible – AxelH Jun 25 '19 at 05:45
  • @AxelH Do you work with project for tests? – Ori Marko Jun 25 '19 at 05:48
  • I never used that approach... if this is your question. – AxelH Jun 25 '19 at 05:49

2 Answers2

2

In my opinion, the simplest solution is to first delete or exclude test package from your project, then either utilize some tool that finds all the unused methods, or update the Java Compiler Error/Warning settings for unused/unnecessary code to get some errors/warnings as a result for you after a build.

I couldn't find any unused method finder tool where you can exclude some usages from certain packages. If there is any, I'd still recommend above steps with compiler, for I'd rather depend on less tools on my IDE, if tool adds very little to the productivity.

buræquete
  • 14,226
  • 4
  • 44
  • 89
  • I am looking for something similar to Intellij feature that shows unused functions are faded. – fuat Jul 29 '21 at 09:25
0

I think a more direct answer would be use DeadCodeDetector see: https://github.com/evernat/dead-code-detector/wiki

First, run it including both your src/main and src/test classes and output to an XML log. Second, run it including ONLY your src/main, and output to a second XML log.

The difference between those two will be the methods that are only called in tests.

This is the approach I'm taking, in removing a rather large subsystem that is no longer needed. If there are new unused methods after pruning that subsystem, then they were only referenced by that subsystem and they can also be removed.

Scott Carlson
  • 3,764
  • 1
  • 17
  • 11