Last active
June 30, 2017 18:20
-
-
Save kawaveri/22fca08ef6c11c49da0d62b8bcf75b88 to your computer and use it in GitHub Desktop.
けものはいても、のけものはいないことを検証します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.sub.zico_hihan.javaripark | |
import jp.sub.zico_hihan.javaripark.JavariPark | |
import org.junit.Assert.* | |
import org.junit.Test | |
import org.junit.Before | |
/** | |
* Example local unit test, which will execute on the development machine (host). | |
* | |
* See [testing documentation](http://d.android.com/tools/testing). | |
*/ | |
class ExampleUnitTest { | |
var javariPark: JavariPark? = null | |
@Before | |
fun setUp() { | |
javariPark = JavariPark() | |
} | |
@Test | |
fun けものはいる() { | |
assertTrue(javariPark!!.exists("けもの")) | |
} | |
@Test | |
fun セルリアンもいる() { | |
assertTrue(javariPark!!.exists("セルリアン")) | |
} | |
@Test | |
fun のけものはいない() { | |
assertFalse(javariPark!!.exists("のけもの")) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.sub.zico_hihan.javaripark | |
class JavariPark { | |
fun exists(s: String): Boolean { | |
if (s == "けもの") { | |
return true | |
} | |
if (s == "セルリアン") { | |
return true | |
} | |
return false | |
} | |
} |
『「JavariPark」クラス内の「exists()」メソッドに"けもの"という文字を渡すとTrueが返ってくる』という結果を期待したテスト「けものはいる()」を記述。
最初の「setUp()」は初期設定。「@before」の部分で、「テスト実行前にjavariParkオブジェクトを初期化して下さい」とお願いしてます。
「JavariPark」なんてクラスは存在しないので、当然レッド(エラー)となります。
trueを返すメソッド「exists()」を含むクラス「JavariPark」を追加作成。
これで先程のテスト「けものはいる()」を再実行するとグリーン(テストOK)が返ってきます。
もう一つのテスト、「のけものはいない()」を追加。
これも失敗するので、「けもの以外はfalseを返す」ようにプログラムの方を修正。
「セルリアンもいる()」パターンのテストを追加。
exists()メソッドをリファクタリング。
重複する判定式を配列にまとめてスッキリ。
ExampleUnitTestクラスもリファクタリング。
JavariParkオブジェクトの初期化をKotlinらしくスッキリさせてみた。
定数を別ファイル管理にしました。
コメント記載したところで、取り敢えず完成。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
プロジェクト新規作成。
「4と2+2は同じです」という式が書かれたテストが最初から入ってました。
実行するとグリーン(テストOK)が返ってきます。