diff --git a/contents/tutorial/coding-java-junit/README.md b/contents/tutorial/coding-java-junit/README.md new file mode 100644 index 00000000..eb0ca0c0 --- /dev/null +++ b/contents/tutorial/coding-java-junit/README.md @@ -0,0 +1,10 @@ +整数 `x` と `y` を引数で受け取り、足し算した値を戻り値として返す `add(int x, int y)` メソッドを実装してください。 + + +## 例 + +``` java +add(2, 3) // 5 +add(15, 8) // 23 +add(65, 94) // 159 +``` diff --git a/contents/tutorial/coding-java-junit/build.gradle b/contents/tutorial/coding-java-junit/build.gradle new file mode 100644 index 00000000..7a515980 --- /dev/null +++ b/contents/tutorial/coding-java-junit/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0' + testImplementation 'org.hamcrest:hamcrest:2.1' +} + +test { + useJUnitPlatform() + + testLogging { + showStandardStreams true + events 'failed' + exceptionFormat 'full' + } + + def index = 1 + afterTest { desc, result -> + def str = result.resultType == org.gradle.api.tasks.testing.TestResult.ResultType.SUCCESS ? "ok" : "not ok" + logger.quiet "${str} ${index++} ${desc.displayName}" + } +} diff --git a/contents/tutorial/coding-java-junit/solution.md b/contents/tutorial/coding-java-junit/solution.md new file mode 100644 index 00000000..8bc65375 --- /dev/null +++ b/contents/tutorial/coding-java-junit/solution.md @@ -0,0 +1 @@ +`x + y`を返すように修正します diff --git a/contents/tutorial/coding-java-junit/solution/App.java b/contents/tutorial/coding-java-junit/solution/App.java new file mode 100644 index 00000000..82bd02ba --- /dev/null +++ b/contents/tutorial/coding-java-junit/solution/App.java @@ -0,0 +1,5 @@ +public class App { + public static int add(int x, int y) { + return x + y; + } +} diff --git a/contents/tutorial/coding-java-junit/src/main/java/App.java b/contents/tutorial/coding-java-junit/src/main/java/App.java new file mode 100644 index 00000000..f318b3c4 --- /dev/null +++ b/contents/tutorial/coding-java-junit/src/main/java/App.java @@ -0,0 +1,5 @@ +public class App { + public static int add(int x, int y) { + return -1; + } +} diff --git a/contents/tutorial/coding-java-junit/src/test/java/AppTest.java b/contents/tutorial/coding-java-junit/src/test/java/AppTest.java new file mode 100644 index 00000000..d40b6077 --- /dev/null +++ b/contents/tutorial/coding-java-junit/src/test/java/AppTest.java @@ -0,0 +1,38 @@ +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.DisplayName; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.is; + +@Timeout(5) +public class AppTest { + @Test + @DisplayName("[基本実装] 入力が 2 と 3 のとき、期待された出力結果が得られる") + public void test_2_3() { + int[] input = {2, 3}; + __testOutput(input, 5); + } + + @Test + @DisplayName("[基本実装] 入力が 15 と 8 のとき、期待された出力結果が得られる") + public void test_15_8() { + int[] input = {15, 8}; + __testOutput(input, 23); + } + + @Test + @DisplayName("[基本実装] 入力が 65 と 94 のとき、期待された出力結果が得られる") + public void test_65_94() { + int[] input = {65, 94}; + __testOutput(input, 159); + } + + private void __testOutput(int[] input, int expected) { + int x = input[0]; + int y = input[1]; + int result = Util.runApp(x, y); + + assertThat(result, is(expected)); + } +} diff --git a/contents/tutorial/coding-java-junit/src/test/java/Util.java b/contents/tutorial/coding-java-junit/src/test/java/Util.java new file mode 100644 index 00000000..80bee2e8 --- /dev/null +++ b/contents/tutorial/coding-java-junit/src/test/java/Util.java @@ -0,0 +1,35 @@ +import static org.junit.jupiter.api.Assertions.fail; + +public class Util { + private static long TIMEOUT = 5_000; + + public static int runApp(int x, int y) { + AppThread th = new AppThread(x, y); + try { + th.start(); + th.join(TIMEOUT); + } catch (InterruptedException e) { + fail("Timeout " + TIMEOUT + "ms"); + } + return th.getResult(); + } + + private static class AppThread extends Thread { + private final int x; + private final int y; + private int result = -1; + + AppThread(int x, int y) { + this.x = x; + this.y = y; + } + + int getResult() { + return this.result; + } + + public void run() { + this.result = App.add(x, y); + } + } +} diff --git a/contents/tutorial/coding-java-junit/track.yml b/contents/tutorial/coding-java-junit/track.yml new file mode 100644 index 00000000..cf9dbace --- /dev/null +++ b/contents/tutorial/coding-java-junit/track.yml @@ -0,0 +1,22 @@ +type: coding +main: src/main/java/App.java +editable: + - src/main/java/App.java +readonly: + - src/test/java/AppTest.java + - src/test/java/Util.java + - build.gradle +build: gradle compileTest -q +test: gradle test --warning-mode=none +envConf: + imageName: java + cacheDirs: + - /root/.m2 + - /root/.gradle +solutions: + - src/main/java/App.java:solution/App.java +testcases: + open: 3 + secret: 0 +evaluationPoint: + 基本実装: 基本的なテストケースにおいて、正答が出力できる