0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · 강의

테스트 먼저 작성하기

프로덕션 코드를 구현하기 전에 원하는 동작을 정의하는 실패 테스트를 작성하는 연습을 합니다.

테스트 먼저 작성하기은(는) CoddyKit의 무료 Testing Mastery: JUnit, Mockito & Integration Tests 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Testing Mastery: JUnit, Mockito & Integration Tests 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Testing Mastery: JUnit, Mockito & Integration Tests 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

TDD's 'Red' Phase

Welcome to the core of Test-Driven Development (TDD)! In this lesson, we'll dive into the crucial first step: writing a failing test first.

This is often called the 'Red' phase of the TDD cycle, because when you run your tests, you expect to see a 'red bar' indicating a failure.

Why Test First?

It might feel counter-intuitive to write a test for code that doesn't exist yet, or isn't fully implemented. But this approach offers great benefits:

  • Clarity: It forces you to think about what the code should *do* before you think about *how* to do it.
  • Proof of Failure: It proves your test works correctly by showing it can fail. If it passes without any code, your test might be faulty!
  • Focus: It gives you a clear, immediate goal: make this one test pass.

Tests as Specifications

Think of your failing test as a precise specification or a mini-requirement. It describes a specific behavior your future code must exhibit.

  • It defines the input.
  • It defines the expected output or outcome.
  • It's concrete and executable.

This helps prevent misunderstandings and ensures your code delivers exactly what's needed for that specific scenario.

Scenario: Simple Calculator

Let's practice by building a simple Calculator class. For our first piece of functionality, we want it to be able to add two numbers.

Our goal is to write a test for an add method that will fail before we even write the correct implementation of add.

Designing the Failing Test

Before writing code, let's think:

  • What input? We'll add 2 and 3.
  • What output? We expect the result to be 5.
  • What method? A method called add on a Calculator object.

We'll write a test that calls calculator.add(2, 3) and asserts that the result is 5.

Code Demo: The Failing Test

Here's a simulated example of writing a test first. Notice the Calculator's add method is intentionally incorrect (it returns 0). This makes our 'test' fail, demonstrating the 'Red' phase.

Run this code to see the simulated failure!

public class Main {
    public static void main(String[] args) {
        System.out.println("Running a simulated test...");
        Calculator calculator = new Calculator();

        // Our test scenario: 2 + 3 should be 5
        int expected = 5;
        int actual = calculator.add(2, 3);

        if (expected == actual) {
            System.out.println("Test PASSED: 2 + 3 = " + actual);
        } else {
            System.out.println("Test FAILED: Expected " + expected + " but got " + actual);
            System.out.println("This is our 'Red' phase!");
        }
    }
}

// The production code (intentionally incorrect for 'Red' phase)
class Calculator {
    public int add(int a, int b) {
        return 0; // Wrong implementation to make the test fail
    }
}

Analyzing the 'Red'

After running the previous code, you saw a 'Test FAILED' message. This is exactly what we wanted!

  • Expected: 5
  • Actual: 0

This failure confirms two things: our test is correctly written, and the production code (the add method) does not yet meet the specified behavior.

The 'Minimum Code' Mindset

Once you have a failing test, the next step in TDD (the 'Green' phase) is to write the *simplest possible code* to make that test pass.

  • Don't over-engineer.
  • Don't add extra features.
  • Just focus on satisfying the current failing test.

This keeps your code clean and focused, building functionality incrementally.

Advantages of Test-First

Embracing the 'Red' phase of TDD leads to:

  • Better Design: Tests guide you towards more modular and testable code.
  • Increased Confidence: Each passing test gives you confidence in your code's correctness.
  • Reduced Bugs: Catch issues early, before they become complex problems.
  • Living Documentation: Your tests serve as up-to-date examples of how your code should be used.

Quick Check on TDD Red

Let's test your understanding of the 'Red' phase in Test-Driven Development.

Recap: The 'Red' Phase

You've explored the crucial first step of TDD: writing a failing test.

  • The 'Red' phase means writing a test that fails.
  • This test acts as a clear specification for new functionality.
  • Seeing the test fail confirms its validity and that the code isn't yet correct.
  • This methodical approach leads to clearer requirements and more robust code.

Next, we'll learn how to make that failing test pass – the 'Green' phase!

자주 묻는 질문

“테스트 먼저 작성하기” 강의는 무료인가요?

네 — “테스트 먼저 작성하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Testing Mastery: JUnit, Mockito & Integration Tests 강의 전체를 잠금 해제할 수 있습니다. Testing Mastery: JUnit, Mockito & Integration Tests 강의에는 총 4개의 강의가 포함되어 있습니다.

“테스트 먼저 작성하기”에서 뭘 배우나요?

프로덕션 코드를 구현하기 전에 원하는 동작을 정의하는 실패 테스트를 작성하는 연습을 합니다. 브라우저에서 직접 실행하는 실습 코드로 Testing Mastery: JUnit, Mockito & Integration Tests을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Testing Mastery: JUnit, Mockito & Integration Tests을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Testing Mastery: JUnit, Mockito & Integration Tests은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“테스트 먼저 작성하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Testing Mastery: JUnit, Mockito & Integration Tests 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Testing Mastery: JUnit, Mockito & Integration Tests 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. TDD 주기 소개
  2. 테스트 먼저 작성하기
  3. 테스트 용이성을 위한 리팩터링
  4. TDD의 세 가지 법칙
← Testing Mastery: JUnit, Mockito & Integration Tests(으)로 돌아가기