Search code examples
eclipsejunitfinal

Final JUnit method stub


When would I want to make my test method signatures final? Isn't it bad practice to extend test cases? This seems an unnessesary declaration.

public final void testMyMethod()

This question arose when looking through the options for creating a new JUnit test case in Eclipse.


Solution

  • I wouldn't say it isn't a best practice (even if it is) because no one would care about this. So basically it is just absolutely superfluous.

    But you have asked when you want to make your test methods final. The technical answer is "make it final if you don't want them to be overwritten". But as no one would do this (at least I hope this) you really don't want to make them final because it just slows you down in doing the real work. And yes in my opinion it is bad practice to extend test cases. Those tests are pretty much complicate to understand most of the times.

    I'm always declaring the test classes as final because I don't want anybody to extend them. Maybe there are good reasons for doing this but in the cases where not the other is forced to rethink what he is doing before removing the final declaration.