TDD with JUnit 2

33 %
67 %
Information about TDD with JUnit 2

Published on May 29, 2014

Author: ssusercef361



TDD with JUnit 2
We are the Java Experts ์Šคํ„ฐ๋”” ์ž๋ฃŒ

TDDwith JUnit Test Driven Development Junyoung Lee 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

Never in the ๏ฌeld of software development was so much owed by so many to so few lines of code - Martin Fowler 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

Need ๋ฌด์—‡์„ ํ…Œ์ŠคํŠธ ํ•  ๊ฒƒ์ธ๊ฐ€? Test ์‹คํŒจํ•˜๋Š” ํ…Œ์ŠคํŠธ ์ž‘์„ฑ Code ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑ Refactoring ์ฝ”๋“œ์˜ ์ค‘๋ณต ์ œ๊ฑฐ TDD Flow Remind 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

๋‹จ์œ„ ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๋ช‡ ๊ฐ€์ง€ ๊ทœ์น™ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋Š” ๋‹ค๋ฅธ ๋ชจ๋“  ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋“ค๊ณผ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค. ํ”„๋ ˆ์ž„ ์›Œํฌ๋Š” ํ…Œ์ŠคํŠธ ๊ฐ๊ฐ์˜ ์˜ค๋ฅ˜๋ฅผ ์‹๋ณ„ํ•˜๊ณ  ๋ณด๊ณ ํ•ด์•ผ ํ•œ๋‹ค. ์–ด๋–ค ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ง€ ์„ ํƒํ•˜๊ธฐ ์‰ฌ์›Œ์•ผ ํ•œ๋‹ค. 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

JUnit์˜ ์„ค๊ณ„ ๋ชฉํ‘œ ์œ ์šฉํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋ณดํƒฌ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ์‹œ๊ฐ„์ด ์ง€๋‚˜๋„ ๊ฐ€์น˜๊ฐ€ ๋ณ€์น˜ ์•Š๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋ณดํƒฌ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ํ†ตํ•ด ํ…Œ์ŠคํŠธ ์ž‘์„ฑ ๋น„์šฉ์„ ๋‚ฎ์ถ”๋Š” ๋ฐ ๋ณดํƒฌ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

๊ณ„์‚ฐ๊ธฐ ํด๋ž˜์Šค์—์„œ ์‹œ์ž‘ํ•˜๊ธฐ public class Calculator { public double add(double number1, double number2) { return number1 + number2; } } 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

public static void main(String[] args) { Calculator calculator = new Calculator(); double result = calculator.add(10, 50); if (result != 60) { System.out.println("Bad result: " + result); } } ๊ฐ„๋‹จํ•œ add ๋ฉ”์„œ๋“œ ํ…Œ์ŠคํŠธ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

๊ฐœ์„ ๋œ add ๋ฉ”์„œ๋“œ ํ…Œ์ŠคํŠธ private int nbErrors = 0; public void testAdd() { Calculator calculator = new Calculator(); double result = calculator.add(10, 50); if (result != 60) { throw new IllegalStateException("Bad Request: " + result); } } public static void main(String[] args) { CalculatorTest test = new CalculatorTest(); try { test.testAdd(); } catch (Throwable e) { test.nbErrors++; e.printStackTrace(); } if (test.nbErrors > 0) { throw new IllegalStateException("There were " + test.nbErrors + " error(s)"); } } 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

import org.junit.Test; import static org.junit.Assert.*; public class CalculatorTest { @Test public void testAdd() { Calculator calculator = new Calculator(); double result = calculator.add(10, 50); assertEquals(60, result, 0); } } JUnit์„ ์ ์šฉํ•œ add ๋ฉ”์„œ๋“œ ํ…Œ์ŠคํŠธ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ


 ย ํ•ต์‹ฌ

 ย ๋“ค์—ฌ๋‹ค

 ย ๋ณด๊ธฐ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

JUnit์˜ ํ•ต์‹ฌ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๊ฐ€ ๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด - public ํด๋ž˜์Šค - ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ ๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด - @Test - public ๋ฉ”์„œ๋“œ - ํŒŒ๋ผ๋ฏธํ„ฐ X - ๋ฐ˜ํ™˜ํ˜•์€ void 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

JUnit์˜ ํ•ต์‹ฌ JUnit์€ ๊ฐ @Test ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋งˆ๋‹ค ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค. 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

JUnit์˜ ํ•ต์‹ฌ ํ…Œ์ŠคํŠธ ๊ฒ€์ฆ์—๋Š” Assert ํด๋ž˜์Šค์— ์ •์˜๋œ assert ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ assertXxx ๋ฉ”์„œ๋“œ assertArrayEquals(โ€œmessageโ€, A, B) assertEquals(โ€œmessageโ€, A, B) assertSame(โ€œmessageโ€, A, B) assertTrue(โ€œmessageโ€, A) assertNotNull(โ€œmessageโ€, A) ... 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค(ํ˜น์€ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค) @Test๊ฐ€ ๋ถ€์—ฌ๋œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜ ์ด์ƒ ํฌํ•จํ•œ ํด๋ž˜์Šค. ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ์‹œํ—˜ํ•˜๋Š” ํ…Œ์ŠคํŠธ๋“ค์„ ํ•จ๊ป˜ ๋ฌถ๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ํ…Œ์ŠคํŠธ๋Š” @Test๊ฐ€ ๋ถ€์—ฌ๋œ ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•˜๊ณ , ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•œ ํด๋ž˜์Šค๋ฅผ ๋งํ•จ. ๋Œ€๊ฐœ ํ…Œ์ŠคํŠธํ•  ํด๋ž˜์Šค ํ•˜๋‚˜ ๋‹น ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ฆ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ ํ…Œ์ŠคํŠธ๋“ค์˜ ์ง‘ํ•ฉ. ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ๋Š” ๊ด€๋ จ๋œ ํ…Œ์ŠคํŠธ๋“ค์„ ํ•จ๊ป˜ ๋ฌถ๋Š” ํŽธ๋ฆฌํ•œ ์ˆ˜๋‹จ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์— ๋ณ„๋„์˜ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด, JUnit์€ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋‚ด์˜ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค. 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

ํ…Œ์ŠคํŠธ ๋Ÿฌ๋„ˆ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ ์‹คํ–‰ ์—”์ง„. JUnit์—๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋Ÿฌ๋„ˆ๊ฐ€ ์ค€๋น„๋˜์–ด ์žˆ๋‹ค. ์ถ”ํ›„ ์ปค์Šคํ…€ ๋Ÿฌ๋„ˆ๋ฅผ ์ œ์ž‘ํ•  ์˜ˆ์ •. 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

Parameterized ํ…Œ์ŠคํŠธ @RunWith(Parameterized.class) public class ParameterizedTest { private double expected; private double valueOne; private double valueTwo; @Parameters public static Collection<Integer[]> getTestParameters() { return Arrays.asList(new Integer[][]{ {2, 1, 1}, // ์˜ˆ์ƒ๊ฐ’, ๊ฐ’1, ๊ฐ’2 {3, 2, 1}, // ์˜ˆ์ƒ๊ฐ’, ๊ฐ’1, ๊ฐ’2 {4, 3, 1}, // ์˜ˆ์ƒ๊ฐ’, ๊ฐ’1, ๊ฐ’2 }); } public ParameterizedTest(double expected, double valueOne, double valueTwo) { this.expected = expected; this.valueOne = valueOne; this.valueTwo = valueTwo; } @Test public void sum() { Calculator calc = new Calculator(); assertEquals(expected, calc.add(valueOne, valueTwo), 0); } } 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

Parameterized ํ…Œ์ŠคํŠธ ๋Ÿฌ๋„ˆ ์‚ฌ์šฉ ์กฐ๊ฑด ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์— @RunWith ์• ๋…ธํ…Œ์ด์…˜ ๋ถ€์ฐฉ @RunWith(Parameterized.class) @Parameters ๋ฉ”์„œ๋“œ ํ•„์š” public static Collection<?[]> xxx() ์ปฌ๋ ‰์…˜์— ๋“ค์–ด๊ฐ€๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” ์ƒ์„ฑ์ž์— ์ „๋‹ฌ๋  ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ธธ์ด์™€ ๊ฐ™์•„์•ผ ํ•จ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

JUnit์ด ์ œ๊ณตํ•˜๋Š” ํ…Œ์ŠคํŠธ ๋Ÿฌ๋„ˆ org.junit.internal.runners.JUnit38ClassRunner org.junit.runners.JUnit4 org.junit.runners.Parameterized org.junit.runners.Suite ํŠน์ • Runner ์ง€์ • @RunWith(๋Ÿฌ๋„ˆ ํด๋ž˜์Šค ์ด๋ฆ„.class) 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

JUnitCore ํผ์‚ฌ๋“œ ํ…Œ์ŠคํŠธ ์ˆ˜ํ–‰, ๊ฒฐ๊ณผ ์ทจํ•จ, ํ†ต๊ณ„ ์ œ๊ณต ํ…Œ์ŠคํŠธ์— ์‚ฌ์šฉํ•  ๋Ÿฌ๋„ˆ๋ฅผ ์ž๋™์œผ๋กœ ์„ ์ • JUnitCore๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ •๋ณด๋ฅผ Eclipse์˜ ํ”Œ๋Ÿฌ๊ทธ์ธ์—์„œ GUI ๋ทฐ๋กœ ๋ณด์—ฌ์คŒ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

์Šค์œ„ํŠธ๋ฅผ ์ด์šฉํ•œ ํ…Œ์ŠคํŠธ ์กฐ์งํ•˜๊ธฐ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๊ฐ€ ํ•˜๋‚˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด..? ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ํ•˜๋‚˜ํ•˜๋‚˜ ์ •์„ฑ๋“ค์—ฌ ์‹คํ–‰? ๊ธฐ๋ณธ ํ…Œ์ŠคํŠธ ๋Ÿฌ๋„ˆ ๊ธฐ๋ณธ ์Šค์œ„ํŠธ ์ƒ์„ฑ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ์‹คํ–‰ 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

์Šค์œ„ํŠธ๋ฅผ ์ด์šฉํ•œ ํ…Œ์ŠคํŠธ ์กฐ์งํ•˜๊ธฐ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ ์ž‘์„ฑ @RunWith(Suite.class) @SuiteClassed({Classes..}) ์Šค์œ„ํŠธ๋Š” ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋‚ด์˜ ๋ชจ๋“  @Test ๋ฉ”์„œ๋“œ๋“ค์„ ์ฐพ์•„ ์‹คํ–‰ํ•˜๋Š” ์ผ์ข…์˜ ๋Ÿฌ๋„ˆ. @RunWith(Suite.class) @SuiteClasses({ CalculatorTest.class, ParameterizedTest.class}) public class AllTests { } 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

์Šค์œ„ํŠธ์˜ ์Šค์œ„ํŠธ @RunWith(Suite.class) @SuiteClasses({CalculatorTest.class}) public class TestSuiteA { } @RunWith(Suite.class) @SuiteClasses({ParameterizedTest.class}) public class TestSuiteB { } @RunWith(Suite.class) @SuiteClasses({TestSuiteA.class, TestSuiteB.class}) public class MasterTestSuite { } 14๋…„ 5์›” 28์ผ ์ˆ˜์š”์ผ

Add a comment

Related presentations

Presentaciรณn que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generarรก el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

JUnit - About

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing ... JUnit 5 - Milestone 2 is available.
Read more

Unit testing Unit testing TDD with JUnit. Unit Testing ...

Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
Read more

Test-driven development JUnit-Test

Einleitung TDD Junit4 Beispiel Inhalt Test-driven Development (TDD) Junit4 Junit & eclipse TestCase TestSuite Die Assert Methoden
Read more

TDD with JUnit - NewCircle Training

Test-Driven Development with JUnit; ... 6 Steps to Get Top Performance from the Changes in Spark 2.0 ... Our TDD course provides an introduction to one ...
Read more

Test Driven Development (TDD): Best Practices Using Java ...

In the previous article Test Driven Development (TDD): Example Walkthrough an example of TDD was given. It went from writing first test and its ...
Read more

TDD: XUnit โ€“ THM-Wiki

TDD und XUnit. Testgetriebene Entwicklung auch im englischen test-driven development TDD genannt ist eine Methode um Computergrogramme zu Entwickeln.
Read more

Introduction to Java TDD - part 1 -

Introduction to TDD with JUnit. Main test driven development principles. Theory and examples provided not only for Java but also for any programming language.
Read more

TDD with Junit - YouTube

TDD with Junit Sujit Kumar. Subscribe Subscribed Unsubscribe 759 759. Loading ... Java Example 2 - Duration: 12:58. in28minutes 6,020 views. 12:58
Read more