Back to feed
Renewal·서른의 생활코딩

Practicing TDD in Spring Boot - Chapter 2. Corrupted Object

NS
normalstory
cover image

Practicing TDD in STS 4.0.1 (Spring Boot)

TDD (Test Driven Development) Hands-on

 

Introduction. Most people can bring out their full potential by following these two simple rules.

1. Before writing any code, write a failing automated test.

2. Eliminate duplication.


Requirements and improvement list

- $5 + 10CHF = $10 (when exchange rate is 2:1)

- $5 x 2 = $10

- Make amount private

- Dollar side effect? <- goal of this example

- Money rounding?

Purpose. Understanding the rhythm of test-driven development

1. Quickly add a test.

2. Run all tests and confirm that the new one fails.

3. Change the code a little.

4. Run all tests and confirm that they all pass.

5. Eliminate duplication through refactoring.


Chapter 2. Corrupted Object The title.. :> As you keep reading, you start to feel Kent Beck's unique wit.

1. The process of getting real code The goal of this example is to resolve the Dollar side effect. When you run the tests consecutively, an error pops up as shown in the screenshot.

1) Adding a second test

Looking at the error, the second test expected 15, but JUnit returned 30. Looking at the code, we can guess that the five object is no longer 5, but has become 10 after the first test.

2) The improvement process

(1) Step 1 - a failing test: modify so the result of the operation is placed into a separate product object rather than into five

 
public class Chapter2 {
	@Test
	public void testMultiplication() {
		Dollar five = new Dollar(5);
		Dollar product; 
		
		//1차 조건, 테스트
		product = five.times(2);
		assertEquals(10, product.amount);
		
		//2차 조건, 테스트
		product = five.times(3);
		assertEquals(15, product.amount);		
	}
}

(2) Step 2 - stub implementation: just do enough so it compiles

 
class Dollar{
	int amount;
	
	Dollar(int amount){
		this.amount = amount;
	}
	
	Dollar times(int multiplier) {
		amount *= multiplier;
                return null;
	}
}

(3) Step 3 - real implementation: replace with correct code

 
public class Chapter2 {
	@Test
	public void testMultiplication() {
		Dollar five = new Dollar(5);
		Dollar product; 
		
		//1차 조건, 테스트
		product = five.times(2);
		assertEquals(10, product.amount);
		
		//2차 조건, 테스트
		product = five.times(3);
		assertEquals(15, product.amount);		
	}
}

class Dollar{
	int amount;
	
	Dollar(int amount){
		this.amount = amount;
	}
	
	Dollar times(int multiplier) {
		return new Dollar(amount * multiplier);
	}
}

3) The result of the fix

2. Lessons

1) Ways to reach the green bar as quickly as possible

(1) Fake implementation: have it return a constant, then gradually turn the constant into a variable until you arrive at real code. This is the so-called stub implementation (see the footnote at the bottom of the linked page).

(2) Use obvious implementation: just type in the real implementation.

2) Summary of Chapters 1-2

(1) Turn a design flaw (Dollar side effect) into a test that fails because of that flaw

(2) Quickly pass compilation with a stub implementation

(3) Enter what you believe is the correct code and get the test to pass

This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min