Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

チュートリアル Javaコーディング問題(JUnit版) #41

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions contents/tutorial/coding-java-junit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
整数 `x` と `y` を引数で受け取り、足し算した値を戻り値として返す `add(int x, int y)` メソッドを実装してください。


## 例

``` java
add(2, 3) // 5
add(15, 8) // 23
add(65, 94) // 159
```
26 changes: 26 additions & 0 deletions contents/tutorial/coding-java-junit/build.gradle
Original file line number Diff line number Diff line change
@@ -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}"
}
}
1 change: 1 addition & 0 deletions contents/tutorial/coding-java-junit/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`x + y`を返すように修正します
5 changes: 5 additions & 0 deletions contents/tutorial/coding-java-junit/solution/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class App {
public static int add(int x, int y) {
return x + y;
}
}
5 changes: 5 additions & 0 deletions contents/tutorial/coding-java-junit/src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class App {
public static int add(int x, int y) {
return -1;
}
}
38 changes: 38 additions & 0 deletions contents/tutorial/coding-java-junit/src/test/java/AppTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
35 changes: 35 additions & 0 deletions contents/tutorial/coding-java-junit/src/test/java/Util.java
Original file line number Diff line number Diff line change
@@ -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();
snakazawa marked this conversation as resolved.
Show resolved Hide resolved
}

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);
}
}
}
22 changes: 22 additions & 0 deletions contents/tutorial/coding-java-junit/track.yml
Original file line number Diff line number Diff line change
@@ -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:
基本実装: 基本的なテストケースにおいて、正答が出力できる