From acca74bf3f36dfb5485bc7beae6e706a8d18be5c Mon Sep 17 00:00:00 2001 From: shino51 Date: Mon, 1 Jan 2024 19:25:33 +0100 Subject: [PATCH] =?UTF-8?q?#71=20tearDown=20=E3=82=92=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=81=97=E3=80=81=E3=83=95=E3=83=A9=E3=82=B0=E3=81=AE=E4=BB=A3?= =?UTF-8?q?=E3=82=8F=E3=82=8A=E3=81=ABlog=E3=81=A7=E7=B5=90=E6=9E=9C?= =?UTF-8?q?=E3=82=92=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codepolaris/tdd/chapter2/TestCase.java | 11 +++++++--- .../org/codepolaris/tdd/chapter2/WasRun.java | 20 ++++++++++++++++--- .../tdd/chapter2/TestCaseTest.java | 16 ++++----------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/shino/src/main/java/org/codepolaris/tdd/chapter2/TestCase.java b/shino/src/main/java/org/codepolaris/tdd/chapter2/TestCase.java index cde6770..6ef06cc 100644 --- a/shino/src/main/java/org/codepolaris/tdd/chapter2/TestCase.java +++ b/shino/src/main/java/org/codepolaris/tdd/chapter2/TestCase.java @@ -18,19 +18,24 @@ public TestCase(String testName) { } public void setUp() { - this.setUp = true; + // empty method + } + + public void tearDown() { + // empty method } public void run() { try { - this.setUp(); + setUp(); + // this is java way to call method from the test name Method method = this.getClass().getDeclaredMethod(this.testName); method.invoke(this); } catch (NoSuchMethodException ex) { - log.error("The provided method name " + this.testName + "not found"); } catch (Exception ex) { log.error("Error during the method call of " + this.testName + ". The actual error is:" + ex.getMessage()); } + tearDown(); } } diff --git a/shino/src/main/java/org/codepolaris/tdd/chapter2/WasRun.java b/shino/src/main/java/org/codepolaris/tdd/chapter2/WasRun.java index f0ebf72..50bd204 100644 --- a/shino/src/main/java/org/codepolaris/tdd/chapter2/WasRun.java +++ b/shino/src/main/java/org/codepolaris/tdd/chapter2/WasRun.java @@ -7,18 +7,32 @@ public class WasRun extends TestCase { private boolean ran; + // name "log" is already used in TestCase. So storedLog will be used here instead of "log" in the book + private final StringBuilder storedLog; + public WasRun(String testName) { super(testName); + storedLog = new StringBuilder(); } @Override public void setUp() { super.setUp(); - this.ran = false; - this.setUp = true; + storedLog.append("setUp "); + } + + @Override + public void tearDown() { + super.tearDown(); + storedLog.append("tearDown "); } public void testMethod() { - ran = true; + storedLog.append("testMethod "); + } + + public String getStoredLog() { + // return string instead of StringBuilder + return storedLog.toString(); } } diff --git a/shino/src/test/java/org/codepolaris/tdd/chapter2/TestCaseTest.java b/shino/src/test/java/org/codepolaris/tdd/chapter2/TestCaseTest.java index 9d50201..a1635e1 100644 --- a/shino/src/test/java/org/codepolaris/tdd/chapter2/TestCaseTest.java +++ b/shino/src/test/java/org/codepolaris/tdd/chapter2/TestCaseTest.java @@ -9,26 +9,18 @@ class TestCaseTest extends TestCase { private static final String TEST_NAME = "testTemplateMethod"; private static final String TEST_METHOD_NAME = "testMethod"; - private final WasRun test; - /** * Java はUnit Test内でconstructorを呼び出すのが困難な為、 * default constructorを作成の上、必要な値を親クラスであるTestCaseに渡す */ public TestCaseTest() { super(TEST_NAME); - this.test = new WasRun(TEST_METHOD_NAME); - } - - @Test - void testSetup() { - this.test.run(); - assertThat(test.isSetUp()).isTrue(); } @Test - void testRunning() { - this.test.run(); - assertThat(test.isRan()).isTrue(); + void testTemplateMethod() { + var test = new WasRun(TEST_METHOD_NAME); + test.run(); + assertThat(test.getStoredLog()).isEqualTo("setUp testMethod tearDown "); } }