0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AndroidStudio: Junit5 の使い方

Posted at

プロジェクトの作成

プロジェクト名: Calc03

環境

app/build.gradle.kts
(省略)
dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.1")
  (省略)

プログラム

Calculator.kt
package com.example.calc03

class Calculator {

  fun add(a: Int, b: Int): Int {
      return a + b
  }

  fun div(a: Int, b: Int): Double {
      assert(b != 0) { "Division by Zero" }
      return a / b * 1.0
  }
}

テストプログラム

Calculator.kt をテストする。

Calculator にカーソルを置いて、
Ctrl + shift + t
image.png

image.png

次のファイルが作成される。

CalculatorTest.kt
package com.example.calc03

import org.junit.jupiter.api.Assertions.*

class CalculatorTest {

    @org.junit.jupiter.api.Test
    fun add() {
    }
}

次のように改造する

CalculatorTest.kt
package com.example.calc03

import org.junit.Test
import org.junit.jupiter.api.Assertions

class CalculatorTest {

    @Test
    fun add() {

val calculator = Calculator()
        Assertions.assertEquals(5, calculator.add(3, 2), "1 + 1 should equal 2")
    }
}

テスト

結果

Tests passed

image.png

image.png

テストを失敗させる

次のように改造

CalculatorTest.kt
(省略)
Assertions.assertEquals(6, calculator.add(3, 2), "1 + 1 should equal 2")
(省略)
Tests failed

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?