javaeclipsestatic-analysisunused-functions

Java - find methods used only in test (unused in source code)


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)

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?


Solution

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