Skip to content

Commit

Permalink
#71 tearDown を実装し、フラグの代わりにlogで結果を出力するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
shino51 committed Jan 1, 2024
1 parent f93c5fd commit acca74b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
11 changes: 8 additions & 3 deletions shino/src/main/java/org/codepolaris/tdd/chapter2/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
20 changes: 17 additions & 3 deletions shino/src/main/java/org/codepolaris/tdd/chapter2/WasRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
16 changes: 4 additions & 12 deletions shino/src/test/java/org/codepolaris/tdd/chapter2/TestCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
}
}

0 comments on commit acca74b

Please sign in to comment.