Skip to content

Commit

Permalink
#72 テスト結果を表示する為のTestResultクラスをrun()が返すようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
shino51 committed Jan 1, 2024
1 parent acca74b commit e7c8e60
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public void tearDown() {
// empty method
}

public void run() {
public TestResult run() {
TestResult result = new TestResult();
try {
result.testStarted();
setUp();
// this is java way to call method from the test name
Method method = this.getClass().getDeclaredMethod(this.testName);
Expand All @@ -37,5 +39,6 @@ public void run() {
log.error("Error during the method call of " + this.testName + ". The actual error is:" + ex.getMessage());
}
tearDown();
return result;
}
}
18 changes: 18 additions & 0 deletions shino/src/main/java/org/codepolaris/tdd/chapter2/TestResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.codepolaris.tdd.chapter2;

public class TestResult {

int runCount;

public TestResult() {
this.runCount = 0;
}

public void testStarted() {
this.runCount++;
}

public String summary() {
return String.format("%d run, 0 failed", this.runCount);
}
}
4 changes: 4 additions & 0 deletions shino/src/main/java/org/codepolaris/tdd/chapter2/WasRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public void testMethod() {
storedLog.append("testMethod ");
}

public void testBrokenMethod() {
throw new RuntimeException("failed");
}

public String getStoredLog() {
// return string instead of StringBuilder
return storedLog.toString();
Expand Down
15 changes: 15 additions & 0 deletions shino/src/test/java/org/codepolaris/tdd/chapter2/TestCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestCaseTest extends TestCase {

private static final String TEST_NAME = "testTemplateMethod";
private static final String TEST_METHOD_NAME = "testMethod";
private static final String TEST_BROKEN_METHOD_NAME = "testBrokenMethod";

/**
* Java はUnit Test内でconstructorを呼び出すのが困難な為、
Expand All @@ -23,4 +24,18 @@ void testTemplateMethod() {
test.run();
assertThat(test.getStoredLog()).isEqualTo("setUp testMethod tearDown ");
}

@Test
void testResult() {
var test = new WasRun(TEST_METHOD_NAME);
var result = test.run();
assertThat(result.summary()).isEqualTo("1 run, 0 failed");
}

@Test
void testFailedResult() {
var test = new WasRun(TEST_BROKEN_METHOD_NAME);
var result = test.run();
// assertThat(result.summary()).isEqualTo("1 run, 1 failed");
}
}

0 comments on commit e7c8e60

Please sign in to comment.