Lessons Learned from Adding Tests to Legacy Code

I'm adding tests to a five-year-old Spring project, late in the game. At first I thought, 'Why now?' but after two months, I've come to understand why it's necessary in my bones.

If you set coverage as your goal from the start, you're doomed. The moment you aim for 100%, you only end up with meaningless tests. It was right to start with the logic that handles money.

1. For code without tests, first use characterization tests to pin down current behavior

2. For classes with many dependencies, start by cleaning up constructor injection

3. Minimize DB-backed tests and separate out core logic

4. Only refactor after confirming tests pass

The biggest takeaway was that code that's hard to test is mostly a sign that the design is off. In effect, tests became a design review tool.

by 클라우드러버898

7 answers

Agreed

by 호기심천국269 · ▲0

I really agree with capturing the current behavior with characterization tests. But if you capture it wrong, don’t bugs end up getting cemented into the spec? I think you should at least confirm whether the behavior is intended before pinning it down.

by 밤샘코더339 · ▲0

Do you have a source? It seems a bit exaggerated that you felt that physically after only two months. Usually, at that point, it’s still just the first round.

by 취준생김씨13 · ▲0

Cleaning up constructor injection (#2) really has a big impact, but people rarely do it. I've seen several cases where people force Mockito into classes with field injection and end up in testing hell. That said, separating pure logic (#3) takes more time than you'd think, so I vote for the advice to start with the logic where money changes hands.

by 밤샘코더513 · ▲0

Well, that’s a bit much. The claim that you’re doomed if you make coverage a goal is only half true. If a team has no testing culture at all, you need at least a minimum baseline to get moving. Saying “don’t aim for 100%” and saying “we won’t use coverage metrics at all” are not the same thing, are they?

by 디지털노마드259 · ▲0

Code that's hard to test = bad design. I still feel this even in my fifth year. But in reality, when you're fighting deadlines, you end up with "just make it work"—and then you defend that with tests, I guess. When I was adding tests to legacy code, I didn't turn on coverage tools at all. Once people see numbers, they end up working to match the numbers.

by 궁금한사람70 · ▲0

I had a similar experience adding tests to a three-year-old service. For the first two months, it really looked like I was getting nowhere, and my team lead kept asking what I was even doing. But after I set up characterization tests on the payment module, reviewers' comments on refactoring PRs like "this looks like it's going to break" dropped sharply. In the end, the saying that tests are a design review tool resonates with me the most. That said, minimizing DB tests is good, but some failures at transaction boundaries can't be caught with an in-memory DB, so I'd recommend keeping a few integration tests.

by 스타트업러321 · ▲0