1

I have a java project and i'm trying to do TDD.

My problem is when I'm about to change API of one class, I edit its tests, then the class itself. But there might be a bunch of other classes using the first class I edited.

So I would like compile this one class and its dependecies only. And then run the tests in such minimal environment.

Here's an example. My project structure prototype:

.
|_ build.xml
|_ lib/
| |_ *.jar
|_ src/
| |_ myPackage/
|   |_ ClassA.java 
|   |_ ClassB.java
|   |_ ClassC.java
|_ test/
  |_ myPackage/
    |_ ClassATest.java
    |_ ClassBTest.java

ClassA uses ClassC. ClassA.java:

import myPackage.ClassC;
...

Now after the API changes in ClassA I would like to run its tests, but without compiling ClassB. So I want to compile only ClassA and ClassC.

I'm using Ant for builds. I found a similar answer in here. But in this case I have to manually specify which classes to compile:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
    <include name="src/myPackage/ClassA.java"/>
    <include name="src/myPackage/ClassC.java"/>
</javac>

My goals are:

  1. Be able to run all tests separately. E.g. ant test-ClassA.
  2. Make Ant automatically resolve dependencies. Meaning, when I build tests for ClassA it also compiles ClassC.
  3. Make Ant to build test targets automatically for every test suite in test dir. In my case it would be test-ClassA and test-ClassB.

Is it possible to achieve any of those goals with Ant? Or maybe you have better suggestions about running tests?

Community
  • 1
  • 1
PovilasB
  • 1,488
  • 1
  • 15
  • 25
  • How long does it take to rebuild everything? If you have split your application into suitably-sized projects, rebuilding a whole project (which is easy to specify) shouldn't take too long... (Personally I wouldn't use Ant for *every* build anyway... use an IDE with incremental builds during development, and use something like Ant for automated full builds.) – Jon Skeet May 20 '14 at 06:04
  • It's not the build time that I'm concerned about. It's the number of source files I have to adopt to one class changes before I can run the tests. I do not want to edit 10 other files that depende on ClassA just to know that there's a slight bug in ClassA. – PovilasB May 20 '14 at 06:13
  • Well what's that got to do with how you *build*? If you've got 10 files that all depend on ClassA and you change that class in a breaking fashion (e.g. by renaming a method) then you *do* need to make that change in multiple classes. I fail to see how "only building dependencies" would help with that. – Jon Skeet May 20 '14 at 06:14
  • Simply If i had Class1, Class2, Class3, ..., Class10 that uses ClassA. I could rename the method in ClassA, compile only ClassA and run tests for it. If it's good, then I adopt Class1..Class10 files to ClassA changes. – PovilasB May 20 '14 at 06:20
  • 1
    I'd prefer to keep my code compiling at all times - and use automated refactoring for things like renames to keep all the code working together. I suspect your desired approach is relatively unusual, which is why you're finding it hard. – Jon Skeet May 20 '14 at 06:23
  • Well renaming was just an example. Sometimes I have bigger changes in some base class and eventually I have to update several derived classes and even more classes that use those derived classes. And I end up doing a lot of patches before validating my initial changes. Is this really so unusual? – PovilasB May 20 '14 at 06:27
  • Without specific examples I can't really give details, but usually there's a way of keeping the previous behaviour - or you just need to suck it up and make the changes. Yes, this can happen occasionally - but if you're finding it causing you a problem *often*, you might want to look at your design and see if your types are too closely coupled. – Jon Skeet May 20 '14 at 07:12

1 Answers1

1

Make your initial changes to ClassA non-breaking - e.g. if you're changing a method, at first make a copy of the method with a new name but leave the old method unchanged. When you have successfully developed and tested the changes, then you can change the users of ClassA.

Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33