Commits

Shuji Watanabe committed f1d4df9

5章のコードを追加

Comments (0)

Files changed (12)

junit-examples/ch05_list.txt

+第5章のリスト一覧
+------------------
+リスト5.1  src/test/java/ch05/CalcTest.java
+リスト5.2  (なし)
+リスト5.3  src/test/java/ch05/AllTests.java
+リスト5.4  (なし)
+リスト5.5  src/test/java/ch05/ItemStockTest.java
+リスト5.6  src/test/java/ch05/CalcTheoriesTest.java
+リスト5.7  src/test/java/ch05/SlowTests.java
+リスト5.8  src/test/java/ch05/SlowAndFastTest.java
+リスト5.9  src/test/java/ch05/CategoriesTests.java
+
+
+
+

junit-examples/src/main/java/ch05/Calc.java

+package ch05;
+
+public class Calc {
+    public int add(int x, int y) {
+        return x + y;
+    }
+}

junit-examples/src/main/java/ch05/ItemStock.java

+package ch05;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ItemStock {
+
+    private final Map<String, Integer> list = new HashMap<String, Integer>();
+
+    public void add(String item, int num) {
+        int oldSize = size(item);
+        int newSize = oldSize + num;
+        list.put(item, newSize);
+    }
+
+    public int size(String item) {
+        Integer size = list.get(item);
+        return (size != null) ? size : 0;
+    }
+
+    public boolean contains(String item) {
+        return 0 < size(item);
+    }
+
+}

junit-examples/src/test/java/ch05/AllTests.java

+package ch05;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+// リスト5.3
+@RunWith(Suite.class)
+@SuiteClasses({ FooTest.class, BarTest.class })
+public class AllTests {
+}

junit-examples/src/test/java/ch05/BarTest.java

+package ch05;
+
+import org.junit.Test;
+
+public class BarTest {
+    @Test
+    public void Barのテスト() throws Exception {
+    }
+}

junit-examples/src/test/java/ch05/CalcTest.java

+package ch05;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+// リスト5.1
+public class CalcTest {
+
+    @Test
+    public void addに3と4を与えると7を返す() {
+        Calc sut = new Calc();
+        assertThat(sut.add(3, 4), is(7));
+    }
+
+    // エントリーポイント
+    public static void main(String[] args) {
+        org.junit.runner.JUnitCore.main(CalcTest.class.getName());
+    }
+}

junit-examples/src/test/java/ch05/CalcTheoriesTest.java

+package ch05;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+// リスト5.6
+@RunWith(Theories.class)
+public class CalcTheoriesTest {
+    
+    @DataPoints public static int[][] VALUES = {
+            {0, 0, 0},
+            {0, 1, 1},
+            {1, 0, 1},
+            {3, 4, 7},
+    };
+
+    @Theory
+    public void add(int[] values) throws Exception {
+        Calc sut = new Calc();
+        assertThat(sut.add(values[0], values[1]), is(values[2]));
+    }
+}

junit-examples/src/test/java/ch05/CategoriesTests.java

+package ch05;
+
+import org.junit.experimental.categories.Categories;
+import org.junit.experimental.categories.Categories.ExcludeCategory;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite.SuiteClasses;
+
+//リスト5.9
+@RunWith(Categories.class)
+@ExcludeCategory(SlowTests.class)
+@SuiteClasses(SlowAndFastTest.class)
+public class CategoriesTests {
+}

junit-examples/src/test/java/ch05/FooTest.java

+package ch05;
+
+import org.junit.Test;
+
+public class FooTest {
+    @Test
+    public void Fooのテスト() throws Exception {
+    }
+}

junit-examples/src/test/java/ch05/ItemStockTest.java

+package ch05;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+// リスト5.5
+@RunWith(Enclosed.class)
+public class ItemStockTest {
+
+    public static class 空の場合 {
+        ItemStock sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new ItemStock();
+        }
+
+        @Test
+        public void size_Aが0を返す() throws Exception {
+            assertThat(sut.size("A"), is(0));
+        }
+
+        @Test
+        public void contains_Aはfalseを返す() throws Exception {
+            assertThat(sut.contains("A"), is(false));
+        }
+    }
+
+    public static class 商品Aを1件含む場合 {
+        ItemStock sut;
+
+        @Before
+        public void setUp() throws Exception {
+            sut = new ItemStock();
+            sut.add("A", 1);
+        }
+
+        @Test
+        public void sizeが1を返す() throws Exception {
+            assertThat(sut.size("A"), is(1));
+        }
+
+        @Test
+        public void contains_Aはtrueを返す() throws Exception {
+            assertThat(sut.contains("A"), is(true));
+        }
+    }
+}

junit-examples/src/test/java/ch05/SlowAndFastTest.java

+package ch05;
+
+import static org.junit.Assert.*;
+
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+// リスト5.7
+@Ignore("EclipseからJUnitテストを実行した場合、このテストクラスに含まれるテストが実行され、テストが失敗となります")
+public class SlowAndFastTest {
+    @Test
+    public void fastTest_01() throws Exception {
+    }
+
+    @Test
+    @Category(SlowTests.class)
+    public void slowTest_01() throws Exception {
+        fail("CategoriesTestsからは実行されない");
+    }
+
+    @Test
+    @Category(SlowTests.class)
+    public void slowTest_02() throws Exception {
+        fail("CategoriesTestsからは実行されない");
+    }
+}

junit-examples/src/test/java/ch05/SlowTests.java

+package ch05;
+
+// リスト5.7
+public interface SlowTests {
+}