5

Suppose you start with an auto-generated method

public void setKey(Key key) {
    this.key = key;
}

And write a test for it

@Test
public void testSetKey() 

Then 3 month from now you decide that a more appropriate name for the method would be changeKeyTo. You refactor your production code and end up with:

public void changeKeyTo(Key key) {
    this.key = key;
}

Life is good, however, your test name remained unchanged

@Test
public void testSetKey() 

How do you deal with something like this? Can you refactor test code automatically with your production code? Does eclipse allow this?

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 1
    Possible duplicate of http://stackoverflow.com/questions/3317789/is-it-possible-to-automaticly-rename-the-unit-test-class-when-renaming-the-class – nobeh Mar 23 '12 at 15:15
  • 2
    @BehroozNobakht, not a duplicate - this is about renaming a *method* which is also called from tests. This is absolutely necessary, otherwise your test code doesn't compile anymore. Renaming a test *class* similarly to the tested class is a nice to have feature, the absence of which doesn't cause any immediate problem. Your unit tests are still compiling and running perfectly without it, only it is more difficult for you to see the logical dependency between test class and tested class. – Péter Török Mar 23 '12 at 15:19
  • 1
    It's a very good, thoughtful question about the profession, and in an ideal world I might suggest the Programmers venue. One of the things to think about is leaving auto-generated methods intact, since it's nice to have these refreshable with class generation, and doing a wrapper for that if you want "a more appropriate name". But that's a narrow point; I think the Question deserves broader discussion. – hardmath Mar 23 '12 at 15:21
  • Could be. What I understood from the question is that JAM needs to consistently refactor his code including renaming methods and classes that should be also _reflected_ to test classes. A topic that is also discussed in the referred question. – nobeh Mar 23 '12 at 15:22
  • The question deals with refactoring method name in a production code coupled with renaming the method name in your test code. – James Raitsev Mar 23 '12 at 15:29
  • 1
    @JAM: the method *changeKeyTo* may change to *changeKeyIfNullTo* or to *updateKey* or to *adaptKey* or whatever (I'm just making that up), but it *shall not* become *feedTheGorilla* or *cleanTheCar*. If it changes that much, then your test is probably to be rewritten anyway. So I prefer to not make my test method names be changed when I do such refactoring: there are already enough things to verify when doing a commit to not "pollute" the change with test method name changes. I do carefully review every commit and every less line to verify is time saved. But of course YMMV : ) (+1 btw) – TacticalCoder Mar 23 '12 at 15:41
  • @TacticalCoder certainly a valid point. Convention of calling test methods testProdMethodName is nice in theory but very fragile in practice it seems. Thanks! – James Raitsev Mar 23 '12 at 15:48

2 Answers2

1

eclipse would not figure this out to change: It only changes the references of the method used in other classes or in the same class.
If you really want to make this functionality work, you could extend eclipse's refactoring API as I did for my project and give it this new functionality.
If you like to have any references on this just ask me ;-)

GingerHead
  • 8,130
  • 15
  • 59
  • 93
  • 1
    Do tell us more. I believe many people would benefit from how this can be solved – James Raitsev Mar 23 '12 at 15:37
  • 1
    Take a look here and all your questions will be solved: http://stackoverflow.com/questions/9129689/is-there-any-eclipse-refactoring-api-that-i-can-call-programmatically – GingerHead Mar 23 '12 at 15:59
0

Most IDEs with automatic refactoring support will also rename the calls to your method from test code (if you keep your test code in the same project so that the IDE can see it). IIRC Eclipse was able to do that too, the last time I used it. IntelliJ, which I am using now, does it.

Péter Török
  • 114,404
  • 31
  • 268
  • 329