2

I have a Java class with three fields. I realized I only need two of them due to changes in requirements.

Ideally I'd write a failing test case before modifying code.

Is there a standard way, or should I just ignore TDD for this task?

4
  • 1
    If your class implements Serializable interface.. then you should be very sure that serialization doesn't break. Mar 26, 2013 at 9:32
  • What do you want to test exactly? Mar 26, 2013 at 9:32
  • I guess I want to test that a field is NOT present. :-P Mar 26, 2013 at 10:05
  • I could write a test which accesses the field and then throws an exception and set the test to expect another exception or something. I could remove the test once the task is done. I don't know. I do want your opinion on the subject though. Serialization is something I overlooked. Mar 26, 2013 at 10:07

5 Answers 5

3

That's refactoring, so you don't need to start with failing tests.

  1. Find all the methods using the field.
  2. Make sure that they're covered by unit tests.
  3. Refactor the methods so they no longer use the field.
  4. Remove the field.
  5. Ensure that the tests are running.
2
  • 5a Or Remove the test asserting against the field?
    – brumScouse
    Mar 7, 2017 at 20:17
  • @brumScouse I guess, the tests won't compile without that.
    – bereal
    Mar 8, 2017 at 9:25
3

Does the drop of this field change the behavior of the class? If not, just drop the field and check if the class still works correctly (aka, passes the tests you should have already written).
TDD principle is to write code "designed by tests". Which may be sound silly but means that the first class you should write is the test class, testing the behavior of the class under test. You should iterate over few steps:

  1. write the test. It should not compile (you don't have the class/classes under test)
  2. Make the test compile. It should fail (you just have empty class which does not satisfy the assertions in the test)
  3. Make the test to pass in the simplest way (usually, just making the method you are testing to return the expected value)
  4. Refine/Refactor/Generalize the class under test, re-run the test (it should still pass). This step should be really fast, usually less than 2 minutes.
  5. Repeat from step 2 until the desired behavior will emerge almost naturally.
1

If you have an exhaustive list of all the fields you need, you can compare that list of fields by reflection :

yourClassName.getClass().getDeclaredFields() vs your list of fields
2
  • I'm allergic to reflection. ;-) I do avoid reflection at all cost. It makes the code less readable. Mar 26, 2013 at 9:59
  • 1
    I can only think of reflection, otherwise you will fall in compilation errors. I also think that any Unit tests tool checking presence of fields or not will use reflection as well.
    – javadev
    Mar 26, 2013 at 10:05
1

Write a test for the constructor without the field you want to remove.

Obviously only works if the constructor takes the field's value as a parameter.

1
  • Delete all tests covering the removed functionality (this doesn't count as "writing production code" as per the 3 Rules of TDD).

  • Delete all references to the obsolete field in remaining tests. If any of them is to fail, you are then allowed to write the required production code to make it pass.

  • Once your tests are green again, all subsequent modifications fall into the "refactoring" category. You are allowed to remove your (now unused) field here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.