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?

7
  • 1
    Why not just temporarily delete all test packages, and check the unused methods then? Very simple solution without any tool etc. Commented Jun 25, 2019 at 5:01
  • @buræquete I can consider it, but isn't there a simple way as searching can ignore folders? (I have multiple modules) Commented Jun 25, 2019 at 5: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. Commented Jun 25, 2019 at 5:10
  • 1
    Maybe you can just increase the warning level of your unused code in Java compiler settings 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. Commented Jun 25, 2019 at 5: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 Commented Jun 25, 2019 at 5:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am looking for something similar to Intellij feature that shows unused functions are faded.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.