Reputation: 58882
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?
Upvotes: 4
Views: 2916
Reputation: 3814
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.
Upvotes: 0
Reputation: 14698
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.
Upvotes: 2